2016-04-22 139 views
0

好吧,所以基本上我試圖完成的只是使用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循環嵌套循環,以便一次只處理一個數字而不是十個?

回答

0

您發佈的代碼的一個主要問題是您的外部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 
0

這裏是一個暗示:

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 

發表最後一部分,我會幫你調試。