简体   繁体   中英

Two while loops that run simultaneously

I am trying to get the first while True loop to ask a question. If answered with a 'y' for yes, it continues to the second while test_score != 999: loop. If anything other than ay is entered, it should stop. Once the second while loop runs, the user enters their test scores and enters 'end' when they are finished and the code executes, the first "while True" loop should ask if the user would like to enter another set of test scores. If 'y' is entered, then it completes the process all over again. I know the second one is a nested while loop. However, I have indented it, in PyCharm, per python indentation rules and it still tells me I have indentation errors and would not run the program. That is the reason they are both lined up underneath each other, below. With that said, when I run it as it is listed below, it gives the following answer:

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

When I answered 'y', it started a continual loop. When I answered 'n', it did give the print statement. However, it also allowed me to enter in my test scores, input end and execute the program. But, it also did not ask the user if they wanted to enter a new set of test scores. Any suggestions on where I am going wrong?

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)

The while loops should be nested to have recurring entries

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)

Output:

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM