Не могу понять это для l oop in Python - PullRequest
0 голосов
/ 19 апреля 2020

Я пытаюсь получить это, чтобы l oop повторялось на сумму, равную "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

Однако код никогда не доходит до этого l oop и перезапускается с начало. Вот первая половина кода, включая l oop.

 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

Что мне здесь не хватает?

1 Ответ

1 голос
/ 19 апреля 2020

Так как вы складываете до того, как получите эти цифры, ваш totalNumStudents всегда всегда равен нулю. Вы должны добавить оператор сложения после того, как получите цифры:

...

        # 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

...

Кстати, поскольку ваш while True l oop запускается только один раз, вам не нужен l oop. Просто удали его.

...