卡在嵌套的While循环中

时间:2020-03-10 03:04:04

标签: python while-loop nested-loops

下面是我的程序的代码。我试图让它问用户是否要输入一组测试分数。如果是,则程序运行,获取测试分数,当用户键入结束时,循环结束,添加分数,然后给出平均值。然后应该询问用户是否要添加另一组测试分数。如果是,它将再次运行。如果不是,它将停止并具有结束语句。

我在主while循环中有第二个while循环。当我运行代码时,它甚至没有运行主while循环。以下是我对问题的回答是(是)时显示的内容。

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循环;它允许用户输入分数,完成后点击“结束”,计算分数,给出平均值,最后询问用户是否要输入另一组测试分数。

有什么建议吗?我有完整的代码,如下面的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)

2 个答案:

答案 0 :(得分:0)

您的内部循环缩进得太远了。 while True:前面有5个空格,而其他所有内容都缩进了4个空格。另外,当我们谈论缩进时,您的break也缩进不正确。

要解决此问题:

  • while True:之前删除一个空格
  • break之前再添加三个空格
  • 要获得奖励积分,还请从嵌套while的5行中的每行开始删除一个空格,以使它们缩进8或12个空格。

提示更新的问题:

查看内部循环的实现。在什么情况下程序会退出该循环以继续进行test_score = ...

答案 1 :(得分:0)

下面编辑的代码可能会有所帮助,这可能是您想要做的。

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)