程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python exercise 5--99 multiplication table

編輯:Python

subject : Print out a 99 Multiplication table

for i in range(1,10): # Define a i Of for loop
for j in range(1,i+1): # Define a j Of for loop
print("%d*%d=%d "%(j,i,i*j),end="") # Print i*j=(i*j) The style of ,end"" The function of is to say that it is not over yet, and there is no need to change lines
print("") # After one execution j Line feed after the cycle of ,print() It has the function of line feed , So we can omit the inside (/n), It can also be written directly as print()

The result is :

Their thinking :

1、 First traversal i The cycle of , Determine the 1-9 Number of numbers

for i in range(1,10): # Traverse 1-9 Value
print(i) # Print the results 

The result is :

 2、 At this time, observe 99 Multiplication table , because i A fixed value for each row has been implemented , You also need to multiply it by a number in each line , The first line is 1*1 The second line is 1*2 and 2*2, because i You can already determine how many times each line is , So defining a variable j, At different times and i Multiply , Is that when i Enter into 1 In the cycle of ,j Output only 1, When i Enter into 2 The cycle is ,j Output only 1 and 2, And so on , When i Output to 9 when ,j Output only 1-9, So this can be confirmed j The traversal range of , Because with i Changing , therefore j The range of range(1,i+1), Why is this i+1 Well ? Because in the process of letting , It's front closed and back open , So the range is what you want the range to be 1 To i, stay range() The function should be written as 1,i+1. Then let him multiply the two to see the result .

for i in range(1,10): # Define a i Of for loop
for j in range(1,10): # according to i Of for loop , Define a j Of for loop
print("%d*%d=%d "%(j,i,i*j)) ## Print i*j=(i*j) The style of 

The output is :

  In this way, we can see that all the required data have been released. Now we need to adjust the style .

3、 At this time, let him not change his career , Keep output on one line , because print By default, line breaks will be executed , So at this time, you can print() Riga end="",end="" The function of is to show that it is not over , Do not perform line feed . Now print and see the results .

for i in range(1, 10): # Define a i Of for loop
for j in range(1, i + 1): # Define a j Of for loop
print("%d*%d=%d " % (j, i, i * j), end="") # Print i*j=(i*j) The style of ,end="" The function of is to say that it is not over yet, and there is no need to change lines 

  At this time, it becomes a line of output , How can he change his career according to his needs ?

4、 Now it's simple , Because this is based on i Print the data of each line , So every time in i To traverse , Just wrap the line .

for i in range(1, 10): # Define a i Of for loop
for j in range(1, i + 1): # Define a j Of for loop
print("%d*%d=%d " % (j, i, i * j), end="") # Print i*j=(i*j) The style of ,end="" The function of is to say that it is not over yet, and there is no need to change lines
print("") # After one execution j Line feed after the cycle of ,print() It has the function of line feed , So we can omit the inside (/n), It can also be written directly as print()

The result is :

         This solves the problem of using python Output 99 The problem of multiplication table , There are several different solutions to this problem , You can try another way


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved