这是我的代码,它输出一个乘法表,但这不是我想要的!
num = int(input("Multiplication using value? : "))
while num <= 10:
i = 1
while i <= num:
product = num*i
print(num, " * ", i, " = ", product, "\n")
i = i + 1
print("\n")
num = num + 1
我基本上是根据1-9的用户输入创建一个乘法表。
例如如果用户输入“ 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
这是我第一次学习Python,我可以在线找到任何帮助,请帮助
答案 0 :(得分:3)
之所以拥有无限循环,是因为您将i
与num
进行比较,同时每次运行都将num
增加。如果您确保i
始终为<= 10
,则会得到所需的输出:
while num <= 10:
i = 1
while i <= 10:
product = num*i
print(num, " * ", i, " = ", product, "\n")
i = i + 1
num = num + 1
print("\n")
答案 1 :(得分:2)
对于此问题,使用for循环更容易。
num = int(input("Multiplication using value? : "))
for left in range(1,num+1): # 1st loop
for right in range(1,num+1): # 2nd loop (nested)
print(left, " * ", right, " = ", left * right)
print() # newline
要了解此问题,请查看两个被乘数:左和右。
左被乘数来自(1-> num),因此是第一个for循环。
然后,对于左被乘数的每个值,右被乘数从(1-> num)开始,因此第二个循环嵌套在第一个循环内。
答案 2 :(得分:1)
您有很多逻辑错误。请查看以下更新的代码:
num = int(input("Multiplication using value : "))
i=1 #you haven't initialized this variable
while i <=num:
j=1
while j <= num:
product = i*j #updated
print(i, " * ", j, " = ", product, "\n") #updated
j = j + 1
print("\n")
i = i + 1
输出(对于输入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
答案 3 :(得分:1)
即使您发布的代码根本不是pythonic的(它非常接近于可以用C语言编写的代码),也几乎可以正常工作:只需进行最小的修改,就可以将其固定如下,以提供预期的输出:< / p>
numInput = int(input("Multiplication using value? : "))
num = 1
while num <= numInput:
i = 1
while i <= numInput:
product = num*i
print(num, " * ", i, " = ", product)
i = i + 1
print("") # no need to add explicit newline character because it is automatically added
num = num + 1
您还可以使用更Python化的方式执行以下操作:
numInput = int(input("Multiplication using value? : "))
for i in range(1,numInput+1):
for j in range(1,numInput+1):
print(i, " * ", j, " = ", i*j)
print("")
答案 4 :(得分:1)
在python中使用while循环的乘法表
num = int(input("enter the number= "))
i = 1
while i<=10:
print(num, "X", i, "=", num * i)
i = i+1
输出
enter the number= 33
33 X 1 = 33
33 X 2 = 66
33 X 3 = 99
33 X 4 = 132
33 X 5 = 165
33 X 6 = 198
33 X 7 = 231
33 X 8 = 264
33 X 9 = 297
33 X 10 = 330
答案 5 :(得分:0)
在Python 3.6+中,可以将f字符串与嵌套的for
循环一起使用:
num = int(input("Multiplication using value? : "))
for i in range(1, num+1):
for j in range(1, num+1):
print(f'{i} * {j} = {i*j}')
Multiplication using value? : 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
答案 6 :(得分:0)
乘法表1到10
for x in range(1,11):
for y in range(1,11):
print(x*y, end='\t')
print()
print()
答案 7 :(得分:0)
输入任意数字即可在10次迭代中获得正常的多重表(命名法)。
num = int(input("Input a number: "))
# use for loop to iterate 10 times
for i in range(1,11):
print(num,'x',i,'=',num*i)
答案 8 :(得分:0)
num = int(input('Enter the number you want the multiplication table for:'))
i=1
while i<=10:
product = num*i
print(product)
i=i+1
print('Thank you')
答案 9 :(得分:0)
使用while
循环创建乘法表如下所示:
b = int(input('Enter the number of the multiplicaction table : '))
print('The multiplication table of '+ str(b) + 'is : ')
a=0
while a<=11:
a=a+1
c= a*b
print( str(b)+' x '+str(a)+' ='+str(c))
print('done!!!!')
答案 10 :(得分:-1)
在python中创建乘法表:
name=int(input("Enter the number of multiplication"))
i=1
while(i<=10):
print(str(name)+"X"+str(i)"="+str(name*i))
i=i+1