How do I use while loops to create a multiplication table in python?

≯℡__Kan透↙ 提交于 2019-12-11 11:10:18

问题


This is my code, it outputs a multiplication table but it's not what I wanted!

 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

I am basically creating a multiplication table from the user's input from 1-9.

Ex. If the user inputs "3"

I should get this output:

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

This is my first time learning Python, I could find any help online, Pls Help


回答1:


The reason why you have an infinite loop on your hands is because you are comparing i to num, while also increasing num on every run. If you make sure i is always <= 10, you get your desired output:

while num <= 10:
    i = 1
    while i <= 10:
        product = num*i
        print(num, " * ", i, " = ", product, "\n")
        i = i + 1
    num = num + 1
    print("\n")



回答2:


For this problem it's easier to use for loops.

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

To understand this problem, look at the two multiplicands: left and right.

Left multiplicand goes from (1-->num), hence the first for loop.

Then, for each value of the left multiplicand, the right multiplicand goes from (1-->num), hence the 2nd loop is nested inside the first loop.




回答3:


You've lots of logical error. Please have a look at this updated code:

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

Output (for input 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 



回答4:


Even if the code you posted is not pythonic at all (it is very close to what could be written in C language), it nearly works: with minimum modifications, it can be fixed as follows to give your expected ouput:

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

In a more pythonic way, you can also do the following:

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("")



回答5:


In Python 3.6+, you can use f-strings with a nested for loop:

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:


Multiplication table 1 to 10

for x in range(1,11):
  for y in range(1,11):
    print(x*y, end='\t')
  print()
  print()



回答7:


input any number to get your normal multiple table(Nomenclature) in 10 iterate times.

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:


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')


来源:https://stackoverflow.com/questions/51187314/how-do-i-use-while-loops-to-create-a-multiplication-table-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!