卡在嵌套的 While 循环中
Stuck in nested While loop
下面是我的程序的代码。我想让它询问用户是否要输入一组考试成绩。如果是,程序 运行s,获取测试分数,当用户键入 end 时,循环结束,将分数相加,然后给出平均值。然后它应该询问用户是否要添加另一组测试分数。如果是,它会再次 运行s。如果没有,它会停止并有一个结束语。
我在主 while 循环中有第二个 while 循环。当我 运行 代码时,它甚至没有 运行 主 while 循环。以下是我对问题回答 y(是)时显示的内容。
Enter test scores
Enter end to end input
======================
Get entries for another set of scores? y
Enter your information below
Get entries for another set of scores?
它没有运行原来的while循环;让用户输入分数,完成后点击 "end",计算分数,给出平均值,最后询问用户是否要输入另一组测试分数。
有什么建议吗?我有下面 PyCharm 中的完整代码。
print("The Test Scores application")
print()
print("Enter test scores")
print("Enter end to end input")
print("======================")
# initialize variables
counter = 0
score_total = 0
test_score = 0
get_entries = 'y'
while test_score != 999:
while True:
get_entries = input("Get entries for another set of scores? ")
if get_entries == 'y':
print("Enter your information below")
else:
print("Thank you for using the Test Scores application. Goodbye!")
test_score = input("Enter test score: ")
if test_score == 'end':
break
elif (int(test_score) >= 0) and (int(test_score) <= 100):
score_total += int(test_score)
counter += 1
elif test_score == 999:
break
else:
print("Test score must be from 0 through 100. Score discarded. Try again.")
# calculate average score
average_score = round(score_total / counter)
# format and display the result
print("======================")
print("Total Score:", score_total,
"\nAverage Score:", average_score)
你的内循环缩进太多了。 while True:
前面有 5 spaces,而其他所有内容都缩进 4 spaces。此外,当我们谈论缩进时,您有一个 break
也被错误地缩进了。
解决这个问题:
- 从
while True:
之前删除一个space
- 在
break
之前再添加三个space
- 为了加分,还从嵌套
while
内的 5 行中的每一行之前删除一个 space,以便它们缩进 8 或 12 spaces。
更新问题的提示:
查看您的内部循环的实现。在什么情况下程序会退出该循环以继续 test_score = ...
?
下面编辑的代码可能会有所帮助,这可能就是您想要做的。
print("The Test Scores application")
print()
print("Enter test scores")
print("Enter end to end input")
print("======================")
# initialize variables
counter = 0
score_total = 0
test_score = 0
get_entries = 'y'
while test_score != 999:
while True:
get_entries = input("Get entries for another set of scores? ")
if get_entries == 'y':
print("Enter your information below")
else:
print("Thank you for using the Test Scores application. Goodbye!")
break
test_score = input("Enter test score: ")
if test_score == 'end':
break
elif (int(test_score) >= 0) and (int(test_score) <= 100):
score_total += int(test_score)
counter += 1
elif test_score == 999:
break
else:
print("Test score must be from 0 through 100. Score discarded. Try again.")
break
# calculate average score
average_score = round(score_total / counter)
# format and display the result
print("======================")
print("Total Score:", score_total,
"\nAverage Score:", average_score)
下面是我的程序的代码。我想让它询问用户是否要输入一组考试成绩。如果是,程序 运行s,获取测试分数,当用户键入 end 时,循环结束,将分数相加,然后给出平均值。然后它应该询问用户是否要添加另一组测试分数。如果是,它会再次 运行s。如果没有,它会停止并有一个结束语。
我在主 while 循环中有第二个 while 循环。当我 运行 代码时,它甚至没有 运行 主 while 循环。以下是我对问题回答 y(是)时显示的内容。
Enter test scores
Enter end to end input
======================
Get entries for another set of scores? y
Enter your information below
Get entries for another set of scores?
它没有运行原来的while循环;让用户输入分数,完成后点击 "end",计算分数,给出平均值,最后询问用户是否要输入另一组测试分数。
有什么建议吗?我有下面 PyCharm 中的完整代码。
print("The Test Scores application")
print()
print("Enter test scores")
print("Enter end to end input")
print("======================")
# initialize variables
counter = 0
score_total = 0
test_score = 0
get_entries = 'y'
while test_score != 999:
while True:
get_entries = input("Get entries for another set of scores? ")
if get_entries == 'y':
print("Enter your information below")
else:
print("Thank you for using the Test Scores application. Goodbye!")
test_score = input("Enter test score: ")
if test_score == 'end':
break
elif (int(test_score) >= 0) and (int(test_score) <= 100):
score_total += int(test_score)
counter += 1
elif test_score == 999:
break
else:
print("Test score must be from 0 through 100. Score discarded. Try again.")
# calculate average score
average_score = round(score_total / counter)
# format and display the result
print("======================")
print("Total Score:", score_total,
"\nAverage Score:", average_score)
你的内循环缩进太多了。 while True:
前面有 5 spaces,而其他所有内容都缩进 4 spaces。此外,当我们谈论缩进时,您有一个 break
也被错误地缩进了。
解决这个问题:
- 从
while True:
之前删除一个space
- 在
break
之前再添加三个space
- 为了加分,还从嵌套
while
内的 5 行中的每一行之前删除一个 space,以便它们缩进 8 或 12 spaces。
更新问题的提示:
查看您的内部循环的实现。在什么情况下程序会退出该循环以继续 test_score = ...
?
下面编辑的代码可能会有所帮助,这可能就是您想要做的。
print("The Test Scores application")
print()
print("Enter test scores")
print("Enter end to end input")
print("======================")
# initialize variables
counter = 0
score_total = 0
test_score = 0
get_entries = 'y'
while test_score != 999:
while True:
get_entries = input("Get entries for another set of scores? ")
if get_entries == 'y':
print("Enter your information below")
else:
print("Thank you for using the Test Scores application. Goodbye!")
break
test_score = input("Enter test score: ")
if test_score == 'end':
break
elif (int(test_score) >= 0) and (int(test_score) <= 100):
score_total += int(test_score)
counter += 1
elif test_score == 999:
break
else:
print("Test score must be from 0 through 100. Score discarded. Try again.")
break
# calculate average score
average_score = round(score_total / counter)
# format and display the result
print("======================")
print("Total Score:", score_total,
"\nAverage Score:", average_score)