0

I have a problem with this code, this program should keep allowing you enter students until the number of passes reaches 8 or the total number of students reaches 10. However currently it just keeps asking for input and hence there is an infinite loop. How do I go about fixing this?

total_students=0
student_passes=0
student_failures=0

while (total_students <= 10) or (student_passes != 8):
        result=int(input("Input the exam result: "))
        if result>=50:
            student_passes = student_passes + 1
        else:
            student_failures = student_failures + 1
        total_students = total_students + 1

 print (student_passes)
 print (student_failures)

 if student_passes >= 8:
     print ("Well done")
3
  • 2
    change or to and? Commented Oct 10, 2017 at 10:05
  • But I only need either of those conditions to be true to end the loop. Commented Oct 10, 2017 at 10:07
  • 1
    @AbdullahJadoon You need both conditions to be true to stay in the loop. Commented Oct 10, 2017 at 10:07

3 Answers 3

2

Change or to and. While both are true you continue:

total_students=0
student_passes=0
student_failures=0

while (total_students != 10) and (student_passes != 8): # != or <
        result=int(input("Input the exam result: "))
        if result>=50:
            student_passes += 1
        else:
            student_failures += 1
        total_students +=1

print (student_passes)
print (student_failures)
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! Any reason why it waits for 11 inputs until the program terminates? It should be 10.
@AbdullahJadoon We could just change to != 10, as we are incrementing the value. When it hits 10 we dont want to continue. Your error was <= 10, which should be <10
brilliant thanks very much for your help. My work here is done.
@AbdullahJadoon Also notice you can use += 1 to increment, e.g. total_students +=1. Makes things more readable
Oh wow, I didn't know you could do this. This is great, thanks!
|
0

you might have to revisit your code. I m not python expert, however I believe you should modify the condition for while loop.

such as while (total_students <= 10) or (student_passes <= 8): this will resolve your problem.

total_students=0
student_passes=0
student_failures=0

while (total_students <= 10) or (student_passes <= 8):
        result=int(input("Input the exam result: "))
        if result>=50:
            student_passes = student_passes + 1
        else:
            student_failures = student_failures + 1
        total_students = total_students + 1

 print (student_passes)
 print (student_failures)

 if student_passes >= 8:
     print ("Well done")

Comments

0

You should use and instead of or to meet your requirement.

total_students=0
student_passes=0
student_failures=0

while (total_students <= 10 and student_passes < 8):
        result=int(input("Input the exam result: "))
        if result>=50:
            student_passes = student_passes + 1
        else:
            student_failures = student_failures + 1
        total_students = total_students + 1

print (student_passes)
print (student_failures)

if student_passes >= 8:
    print ("Well done")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.