while
我对Python 2.7 中的循环有一个小问题。
我已经定义了一个程序,print_multiplication_table
它以一个正整数作为输入,并打印出一个乘法表,显示所有整数乘法直到并包括输入数。
这是我的print_multiplication_table
功能:
def print_multiplication_table(n):
count = 1
count2 = 1
result = count * count2
print 'New number: ' + str(n)
while count != n and count2 != n:
result = count * count2
print str(count) + " * " + str(count2) + " = " + str(result)
if count2 == n:
count += 1
count2 = 1
else:
count2 += 1
这是一个预期的输出:
>>>print_multiplication_table(2)
new number: 2
1 * 1 = 1
1 * 2 = 2
2 * 1 = 2
2 * 2 = 4
>>>print_multiplication_table(3)
new number: 3
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
一切正常,直到我添加我的while
循环:
while count != n and count2 != n:
现在我的输出如下所示:
>>>print_multiplication_table(2)
New number: 2
1 * 1 = 1
>>>print_multiplication_table(3)
New number: 3
1 * 1 = 1
1 * 2 = 2
我做错了什么,我该如何解决?
谢谢。