python程序的无限循环

时间:2017-10-10 10:03:54

标签: python while-loop infinite-loop

我的代码有问题,这个程序应该允许你进入学生,直到通过次数达到8次或者学生总数达到10次。但是目前它只是一直要求输入,因此有一个无限循环。我该如何解决这个问题?

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 个答案:

答案 0 :(得分:2)

改变或者和。虽然两者都是真的,但你继续:

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)

答案 1 :(得分:0)

您可能需要重新访问您的代码。我不是python专家,但我相信你应该修改while循环的条件。

例如while(total_students&lt; = 10)或(student_passes&lt; = 8): 这将解决您的问题。

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

答案 2 :(得分:0)

您应该使用代替来满足您的要求。

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