简体   繁体   中英

Python While loop inside a for loop only runs once why?

Hello this is my first time posting here. I am new to software development. For days I've been stuck into this problem. I can't make while loop inside for loop run more more once. I normally need two for loops. One for when people answer QQI5 and one for when the answer is QQI6 but not luck. So here I am only trying with one for loop. And the while loop inside. Any help will be appreciated.

studentName = input("Enter Your Name: ")
courseName = input("Enter Course Name: ")
courseLevel = int(input("Enter QQI Level 5 or 6: "))

# Sets variable Data

positiveNumber = True
assignmentsCount = 0
assignmentsTotal = 0


if courseLevel == 6:
    for i in range(3):
        component = input("Enter Component's Name")

        while positiveNumber:
            assignmentToAdd = float(input("Enter assignment score or press -1 to Finish "))
            if assignmentToAdd >= 0:
                assignmentsTotal += assignmentToAdd
                assigmentsCount = +1
            if assignmentToAdd ==100:
                positiveNumber = False
            else:
                positiveNumber = False
        if assignmentsTotal < 50:
            print(assignmentsTotal, "Unsuccessful")
        elif assignmentsTotal >= 50 or assignmentsTotal <65:
            print(assignmentsTotal, "Pass")
        elif assignmentsTotal >= 65 or assignmentsTotal < 80:
            print(assignmentsTotal, "Merit")
        else:
             if assignmentsTotal >= 80:
                print(assignmentsTotal, "Distinction")

Because of this part of your code.

            if assignmentToAdd ==100:
                positiveNumber = False
            else:
                positiveNumber = False

It means assingning False to positiveNumber in any case.

positiveNumber is only True when you enter the while loop , because at the end of the while loop your code says:

        if assignmentToAdd ==100:
            positiveNumber = False
        else:
            positiveNumber = False

So no matter what assignmentToAdd is, at the next iteration-check of your while loop, positiveNumber is False - and thus your while loop will exit.

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