简体   繁体   中英

Can't figure out this For loop in Python

I'm trying to get this For loop to repeat itself for an amount equal to "totalNumStudents"

        for score in range(totalNumStudents):
        # Prompt to enter a test score.
        test_score = int(input("Enter the test score: "))

        # If the test score entered is in range 0 to
        # 100, then break the loop.
        if 0 <= test_score <= 100:
            break

However, the code never gets to this loop and restarts from the beginning. Here is the first half of the code including the loop.

 freshman = 0
sophomore = 0
junior = 0
senior = 0
test_score = 0
totalTestScores = 0
totalNumStudents = freshman + sophomore + junior + senior

# Start a while loop.
while True:
    # Display the menu of choices.
    print("\nPress '1' to enter a student")
    print("Press '2' to quit the program")
    print("NOTE: Pressing 2 will calculate your inputs.\n")

    # Prompt to enter a choice.
    choice = input("Enter your choice: ")

    # If the choice is 1.
    if choice == '1':

        # Start a while loop to validate the grade level
        while True:
            # Prompt the user to enter the required grade level.
            freshman = int(input("How many students are Freshman? "))

            sophomore = int(input("How many students are Sophomores? "))

            junior = int(input("How many students are Juniors? "))

            senior = int(input("How many students are Seniors? "))
            break


        for score in range(totalNumStudents):
            # Prompt to enter a test score.
            test_score = int(input("Enter the test score: "))

            # If the test score entered is in range 0 to
            # 100, then break the loop.
            if 0 <= test_score <= 100:
                break

            # Otherwise, display an error message.
            else:
                print("Invalid test score! Test score " +
                      "must be between 0 and 100.")

        # Calculate total test scores
        totalTestScores = totalTestScores + test_score

What am I missing here?

Since you add up before get those numbers, your totalNumStudents is all always zero. You should put the add up statement after you get the numbers:

...

        # Start a while loop to validate the grade level
        while True:
            # Prompt the user to enter the required grade level.
            freshman = int(input("How many students are Freshman? "))

            sophomore = int(input("How many students are Sophomores? "))

            junior = int(input("How many students are Juniors? "))

            senior = int(input("How many students are Seniors? "))
            break
        # add up here
        totalNumStudents = freshman + sophomore + junior + senior

        for score in range(totalNumStudents):
            # Prompt to enter a test score.
            test_score = int(input("Enter the test score: "))

            # If the test score entered is in range 0 to
            # 100, then break the loop.
            if 0 <= test_score <= 100:
                break

...

BTW since your while True loop only runs once, you don't need the loop. Just remove it.

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