This is the 9th day of my participation in the August Wen Challenge.More challenges in August
Title 1:
For loop nested to print 99 times tables
For I in range(1,10): for j in range(1,10): res = i * j print('{j} * {i} = {res}'.format(i=i,j=j,res=res),end='\t') print()Copy the code
Answer:
1. Let’s print the 1 column first
For I in range(1,10): print('1 x {I} = {res}'. Format (I = I,res=res))Copy the code
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
Copy the code
2. However, we can’t print everything as we go, so we try to print one line at a time. We print 1 horizontally first
For I in range(1,2): print('1 x {I} = {res}'. Format (I = I,res=res))Copy the code
Display:
1 x 1 = 1
Copy the code
3. Then we realize that we need another for loop to replace the number 1, because the number before the print will also change, so we print 1-2
For I in range(1,3): for j in range(1,3): res= I * j print('{j} x {I} = {res}'. Format (I = I,j=j,res=res))Copy the code
Display:
1 x 1 = 1
2 x 1 = 2
1 x 2 = 2
2 x 2 = 4
Copy the code
4. But we found that this prints every time it changes lines, so we left it line-free and split each one with tabs
For I in range (1, 3) : for j in range (1, 3) : res = I * j print (' {j} x {I} = {res} '. The format (I = I, j = j, res = res), end = '\ t')Copy the code
Display:
1 x 1 = 1 2 x 1 = 2 1 x 2 = 2 2 x 2 = 4
Copy the code
5. However, it is displayed on a single line. We want it to be displayed on a new line after the end of the line
For I in range(1,3): for j in range(1,3): res = i * j print('{j} x {i} = {res}'.format(i=i,j=j,res=res),end='\t') print()Copy the code
Display:
1 x 1 = 1 2 x 1 = 2
1 x 2 = 2 2 x 2 = 4
Copy the code
6.1×2 is the same as 2×1, we only want to display 1X2, so we need to optimize it so that the first multiplier j cannot exceed the second multiplier I, which can only be equal to I, so the range of the loop needs to be changed
For I in range(1,3): for j in range(1,3): res = i * j print('{j} x {i} = {res}'.format(i=i,j=j,res=res),end='\t') print()Copy the code
Display:
1 x 1 = 1
1 x 2 = 2 2 x 2 = 4
Copy the code
6. Now you’re almost done, just change the scope to 9×9
For I in range(1,10): for j in range(1,10): res = i * j print('{j} x {i} = {res}'.format(i=i,j=j,res=res),end='\t') print()Copy the code
Display:
1 x 1 = 1
1 x 2 = 2 2 x 2 = 4
1 x 3 = 3 2 x 3 = 6 3 x 3 = 9
1 x 4 = 4 2 x 4 = 8 3 x 4 = 12 4 x 4 = 16
1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25
1 x 6 = 6 2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30 6 x 6 = 36
1 x 7 = 7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35 6 x 7 = 42 7 x 7 = 49
1 x 8 = 8 2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40 6 x 8 = 48 7 x 8 = 56 8 x 8 = 64
1 x 9 = 9 2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45 6 x 9 = 54 7 x 9 = 63 8 x 9 = 72 9 x 9 = 81
Copy the code
Topic 2:
For loop nested print pyramid
*
***
*****
*******
*********
Copy the code
Answer:
This one is a little bit harder, printing this graph, but we’re going to look at the patterns and find as many numbers as we can. Xing_num = 2 * level-1, max_level = 5, xing_num = 2 * level-1, max_level = 5, xing_num = 2 * level-1, max_level = 5, xing_num = 2 * level-1, max_level = 5 So we should use the center function of the string, which increases the number of layers, with each layer having a total length of 9.
For the first layer we set the variable level to 1. We can calculate the number of stars in this layer
level = 1
xing_num = 2 * level - 1
res = '*'*xing_num
Copy the code
2. So we get the image of the star at the first layer, and then we print it out
level = 1
xing_num = 2 * level - 1
res = '*'*xing_num
print(res.center(9))
Copy the code
2. Then we set the layer cycle, cycle print the first three layers
Level = 1 for level in range(1,4): xing_num = 2 * level-1 res = '*'*xing_num print(res.center(9))Copy the code
Results:
* * * * * * * * *Copy the code
3. Since there are five levels, we set max_level = 5 and then change the scope of the loop
level = 1
max_level = 5
for level in range(1,max_level+1):
xing_num = 2 * level - 1
res = '*'*xing_num
print(res.center(9))
Copy the code
Display:
*
***
*****
*******
*********
Copy the code
\