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")
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)
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")
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")
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.