虽然循环乘法表python

虽然循环乘法表python

问题描述:

好吧,所以基本上我试图完成的只是使用while循环并高效地使用它们。嵌套while循环对我来说非常棘手并且很难理解。我正在试图制作一个带有标题的10x10乘法表。虽然循环乘法表python

所以我当前的代码是:

firstNumber = int(input("Please enter the first number: ")) 
secondNumber = int(input("Please enter the second number: ")) 
count = 0 
while(count < 1): 
    print("{:17} {:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5}"\ 
     .format(firstNumber, firstNumber + 1, firstNumber + 2, firstNumber + 3,\ 
       firstNumber + 4, firstNumber + 5, firstNumber + 6, firstNumber\ 
       + 7, firstNumber + 8, firstNumber + 9)) 
    print("{:5} {:}".format(" ", "-"*65)) 
    count += 1 
    counter = 0 
    while(counter < 10): 
     downSolution = firstNumber * secondNumber 
     print("{:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5}"\ 
       .format(secondNumber, "|", downSolution,\ 
        downSolution + secondNumber, downSolution +\ 
        (secondNumber * 2), downSolution + (secondNumber * 3),\ 
        downSolution + (secondNumber * 4), downSolution + \ 
        (secondNumber * 5), downSolution + (secondNumber * 6),\ 
        downSolution + (secondNumber * 7), downSolution + \ 
        (secondNumber * 8), downSolution + (secondNumber * 9))) 
     counter += 1 
     secondNumber += 1 

,输出:

  5  6  7  8  9 10 11 12 13 14 
    ----------------------------------------------------------------- 
5 |  25 30 35 40 45 50 55 60 65 70 
6 |  30 36 42 48 54 60 66 72 78 84 
7 |  35 42 49 56 63 70 77 84 91 98 
8 |  40 48 56 64 72 80 88 96 104 112 
9 |  45 54 63 72 81 90 99 108 117 126 
10 |  50 60 70 80 90 100 110 120 130 140 
11 |  55 66 77 88 99 110 121 132 143 154 
12 |  60 72 84 96 108 120 132 144 156 168 
13 |  65 78 91 104 117 130 143 156 169 182 
14 |  70 84 98 112 126 140 154 168 182 196 

这基本上是正确的,但显然我没有正确地做到这一点。那么,如何更有效地为while循环嵌套循环,以便一次只处理一个数字而不是十个?

您发布的代码的一个主要问题是您的外部while循环实际上只会运行一次。由于您运行的是while(count < 1),并且您在运行循环一次后将count加1,因此此循环在第二次运行时将退出。

尽管您的答案确实打印了正确的乘法表,但它通过打印10个手动编码的结果打破了练习while循环的精神。更聪明的做法是让程序数到10,而不是列出手动填写的每行中的10个字段,如下所示:

firstNumber = int(input("Please enter the first number: ")) 
secondNumber = int(input("Please enter the second number: ")) 

# Print twelve spaces in order to format the columns correctly, but don't 
# place a newline after the print statement 
print(" "*12, end="") 

# Loop through all of the column header values in the first row 
columnCounter = 0 
while columnCounter < 10: 
    # Print a single incremented column value, with a space separating each 
    print("{:5}".format(firstNumber + columnCounter), end=" ") 
    columnCounter = columnCounter + 1 

# Add a newline to go to the next row 
print("") 

# Print the dashes separating the first row from the rest 
print("{:5} {:}".format(" ", "-"*65)) 

# Print the remainder of the rows in the table 
rowCounter = 0 
while(rowCounter < 10): 
    # Print the left-hand side value 
    print("{:5} {:5}".format(secondNumber, "|"), end=" ") 

    downSolution = firstNumber * secondNumber 

    # Loop through the values for all columns for this row 
    columnCounter = 0 
    while(columnCounter < 10): 
     print("{:5}".format(downSolution + secondNumber*columnCounter), end= " ") 
     columnCounter = columnCounter + 1 

    secondNumber = secondNumber + 1 
    print("") 
    rowCounter = rowCounter + 1 

这里是一个暗示:

row = 1 
row_stop = 10 
while row != row_stop: 
    print(row, end = " ") #end = " " adds a spaceafter each print statement 
          #instead of a new line 
    row += 1 

将输出

1 2 3 4 5 6 7 8 9 

第二个循环是棘手的一个吧?一旦你到达行的末尾,它将在11,所以它不会保持循环。如果你有两个循环,但你可以这样做:

row = 1 
row_stop = 10 
col = 1 
col_stop = 10 
while row != row_stop: 
    wile col != col_stop: 
    #print your column stuff 
    #reset your column counter, 
    #increment your row counter 

发表最后一部分,我会帮你调试。