卡在嵌套的 While 循环中

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


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


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


30秒到达战场
浏览 77回答 2
2回答

元芳怎么了

您的内环缩进得太远。 前面有 5 个空格,而其他所有内容都缩进 4 个空格。另外,当我们谈论缩进时,您也有一个不正确的缩进。while True:break要解决此问题,请执行以下操作:从之前删除一个空格while True:在之前再添加三个空格break对于奖励积分,还要从嵌套内的5行之前删除一个空格,以便它们缩进8或12个空格。while更新问题的提示:看看你的内部循环的实现。在什么情况下程序会退出该循环以继续?test_score = ...

天涯尽头无女友

下面编辑的代码可能会有所帮助,这可能就是您想要做的。print("The Test Scores application")print()print("Enter test scores")print("Enter end to end input")print("======================")# initialize variablescounter = 0score_total = 0test_score = 0get_entries = 'y'while test_score != 999:&nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; &nbsp; get_entries = input("Get entries for another set of scores?&nbsp; ")&nbsp; &nbsp; &nbsp; &nbsp; if get_entries == 'y':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Enter your information below")&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Thank you for using the Test Scores application. Goodbye!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; test_score = input("Enter test score: ")&nbsp; &nbsp; &nbsp; &nbsp; if test_score == 'end':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; elif (int(test_score) >= 0) and (int(test_score) <= 100):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; score_total += int(test_score)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter += 1&nbsp; &nbsp; &nbsp; &nbsp; elif test_score == 999:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Test score must be from 0 through 100. Score discarded. Try again.")&nbsp; &nbsp; break&nbsp; &nbsp; # calculate average scoreaverage_score = round(score_total / counter)# format and display the resultprint("======================")print("Total Score:", score_total,&nbsp; &nbsp; &nbsp; "\nAverage Score:", average_score)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python