虽然循环不产生乘法 table
While loop is not producing multiplication table
i = 0
d = input("Enter the no. you want ")
while i < 11 :
print(i * d)
i+= 1
它应该给出 d 的乘法 table 但它给出了例如以下结果。改为“3”
3
33
333
3333
33333
333333
3333333
33333333
333333333
3333333333
input() returns 一个字符串而不是一个整数,如果你将 Python 中的一个字符串乘以一个整数 num
,那么你将得到一个重复的字符串 num次。例如,
s = "stack"
print(s * 3) # Returns "stackstackstack"
您需要使用 int()
构造函数将输入从 str 转换为 int。
d = int(input("Enter the no. you want "))
试试这个:
d = int(input("Enter the no. you want "))
for i in range(1,11):
print(i * d)
在上面的代码中,我用 for loop and range() 替换了你的 while
循环构造以获得数字序列。
奖金:
要print/display table 以更好的方式,请尝试以下代码:
for i in range(1,11):
print("%d X %d = %d" % (d, i, i * d))
输出:
Enter the no. you want 2
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20
i = 0
d = input("Enter the no. you want ")
while i < 11 :
print(i * d)
i+= 1
它应该给出 d 的乘法 table 但它给出了例如以下结果。改为“3”
3
33
333
3333
33333
333333
3333333
33333333
333333333
3333333333
input() returns 一个字符串而不是一个整数,如果你将 Python 中的一个字符串乘以一个整数 num
,那么你将得到一个重复的字符串 num次。例如,
s = "stack"
print(s * 3) # Returns "stackstackstack"
您需要使用 int()
构造函数将输入从 str 转换为 int。
d = int(input("Enter the no. you want "))
试试这个:
d = int(input("Enter the no. you want "))
for i in range(1,11):
print(i * d)
在上面的代码中,我用 for loop and range() 替换了你的 while
循环构造以获得数字序列。
奖金:
要print/display table 以更好的方式,请尝试以下代码:
for i in range(1,11):
print("%d X %d = %d" % (d, i, i * d))
输出:
Enter the no. you want 2
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20