好的,基本上我想要完成的只是使用while循环并有效地使用它们。嵌套while循环对我来说非常棘手并且难以理解。我正在尝试使用标题制作10x10乘法表。
所以我目前的代码是:
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 :(得分: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
发布最后一部分,我会帮你调试。
答案 1 :(得分: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