【问题标题】:How do I use while loops to create a multiplication table?如何使用 while 循环创建乘法表?
【发布时间】:2018-12-13 16:48:29
【问题描述】:

这是我的代码,它输出一个乘法表,但这不是我想要的!

 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

【问题讨论】:

  • 它做了什么不是你想要的?
  • @khelwood 它最多可打印 10 个表格,但我希望它将表格打印到示例中给出的用户输入
  • 如果您在问题中包含实际输出以及预期输出,这将有助于人们理解您的问题。

标签: python python-3.x while-loop


【解决方案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 

【讨论】:

    【解决方案2】:

    即使您发布的代码根本不是pythonic(它非常接近可以用C语言编写的代码),它几乎可以工作:只需最少的修改,它可以按如下方式进行修复以提供您预期的输出:

    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
    

    以更 Pythonic 的方式,您还可以执行以下操作:

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

    【讨论】:

      【解决方案3】:

      对于这个问题,使用 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) 开始,因此第二个循环嵌套在第一个循环内。

      【讨论】:

        【解决方案4】:

        您手上有一个无限循环的原因是因为您将inum 进行比较,同时在每次运行时增加num。如果您确保 i 始终为 &lt;= 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")
        

        【讨论】:

          【解决方案5】:

          在 Python 3.6+ 中,您可以使用带有嵌套 for 循环的 f 字符串:

          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】:

            乘法表1到10

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

            【讨论】:

            • 当您根据for 提出答案时,问题是关于“如何使用while 循环”。如果您认为使用while 循环无法解决问题,您应该解释原因,否则您应该提出一个不使用for 循环的答案。如果是这种情况,您可以解释为什么 wsing while 循环可能例如效率低下。
            【解决方案7】:

            输入任何数字以在 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】:
              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】:

                使用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】:

                  在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
                  

                  【讨论】:

                  • 欢迎来到 Stack Overflow!虽然这段代码可以解决问题,including an explanation 解决问题的方式和原因确实有助于提高帖子的质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
                  【解决方案11】:

                  在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
                  

                  【讨论】:

                    【解决方案12】:
                    num = int(input("Enter the number: "))
                    i = 1
                    
                    print("Mulltiplication of number:", num)
                    
                    while i<=10:
                        print(f"{num}X{i}={num*i}")
                        i = i + 1
                    

                    【讨论】:

                      猜你喜欢
                      • 2016-07-27
                      • 2019-11-08
                      • 1970-01-01
                      • 2012-10-07
                      • 2019-10-11
                      • 2022-12-07
                      • 2015-02-11
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多