Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

Multiplication Table Using Python

for i in range(3,33,3):
    for j in range(1,11,1):
        print("3 *", j, '=', i)
    if j == 10:
       break

This is the output that I am getting:

3 * 1 = 3                                                                                                                                                          
3 * 2 = 3                                                                                                                                                          
3 * 3 = 3                                                                                                                                                          
3 * 4 = 3                                                                                                                                                          
3 * 5 = 3                                                                                                                                                          
3 * 6 = 3                                                                                                                                                          
3 * 7 = 3                                                                                                                                                          
3 * 8 = 3                                                                                                                                                          
3 * 9 = 3                                                                                                                                                          
3 * 10 = 3 

Could anyone please point out the error for me?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you just want to print multiples of 3, you don't need two loops. Just one loop from 1 to 10, and then multiply that by 3.

for i in range(1, 11):
    j = i * 3
    print('3 *', i, '=', j)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...