同时运行的两个while循环

时间:2020-03-12 04:34:15

标签: python while-loop nested break continue

我正在尝试在True循环中第一个问问题。如果用“ y”回答是,则继续执行第二个命令,同时test_score!= 999:循环。如果输入了除y以外的任何其他内容,它将停止。 一旦第二个while循环运行,用户输入他们的测试分数,并在完成测试并执行代码后输入“ end”,第一个“ while True”循环应询问用户是否要输入另一组测试分数。如果输入“ y”,则它将重新完成该过程。我知道第二个是嵌套的while循环。但是,我已经按照python缩进规则在PyCharm中对它进行了缩进,它仍然告诉我我有缩进错误,并且不会运行该程序。这就是为什么它们都在下面彼此排成一行的原因。如此说来,当我按下面列出的方式运行它时,它给出以下答案:

Enter test scores
Enter end to end the input
==========================
Get entries for another set of scores? y
Enter your information below
Get entries for another set of scores? n
((When I type in 'n' for no, it shows the following:))
Thank you for using the Test Scores application. Goodbye!
Enter test score: 80 (I entered this as the input)
Enter test score: 80 (I entered this as the input)
Enter test score: end (I entered this to stop the program)
==========================
Total score: 160
Average score: 80

当我回答“ y”时,它开始了一个连续循环。当我回答“ n”时,它确实给出了打印语句。但是,它也允许我输入测试分数,输入结束并执行程序。但是,它也没有询问用户是否要输入一组新的测试分数。关于我要去哪里哪里有什么建议吗?

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

while True:
    get_entries = input("Get entries for another set of scores?  ")
    if get_entries == 'n':
        print("Thank you for using the Test Scores application. Goodbye!")
        break
    else:
        print("Enter your information below")
        continue

counter = 0
score_total = 0
test_score = 0

while test_score != 999:
    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)

1 个答案:

答案 0 :(得分:0)

while循环应嵌套以包含重复条目

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

while True:
    get_entries = input("Get entries for another set of scores?  ")
    if get_entries == 'n':
        print("Thank you for using the Test Scores application. Goodbye!")
        break
    else:
        print("Enter your information below")
        counter = 0
        score_total = 0
        test_score = 0

        while test_score != 999:
            test_score = input("Enter test a 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)

输出:

The Test Scores application

Enter test scores
Enter end to end input
======================

Get entries for another set of scores?  y
Enter your information below

Enter test a score: 60

Enter test a score: 80

Enter test a score: 90

Enter test a score: 70

Enter test a score: end
======================
Total Score: 300 
Average Score: 75

Get entries for another set of scores?  y
Enter your information below

Enter test a score: 80

Enter test a score: 80

Enter test a score: end
======================
Total Score: 160 
Average Score: 80

Get entries for another set of scores?  n
Thank you for using the Test Scores application. Goodbye!