简体   繁体   English

python while循环不断重复

[英]python while loop keeps on repeating

My problem is that when you answer a question the while loop keeps on repeating until all 10 questions are answered BUT this is only true for getting the answer correct For getting it wrong it loops on forever 我的问题是,当您回答问题时,while循环会一直重复直到所有10个问题都被回答为止,但这仅适用于正确的答案;如果弄错了,它将永远循环

correct answer: 正确答案:

what is
 7  *  2

Answer here: 14
you got the answer right,well done
you got the answer right,well done
you got the answer right,well done
you got the answer right,well done
you got the answer right,well done
you got the answer right,well done
you got the answer right,well done
you got the answer right,well done
you got the answer right,well done
you got the answer right,well done
Welldone you have answered all 10 questions and managed to get correct: 10
press enter to exit

Wrong answer: 错误的答案:

wrong
wrong
wrong

and this repeats on and on and it doesn't end 并不断重复,直到结束

This is the code for the program 这是程序的代码

import random


correct,number,left,right = 0,0,random.randint(6,10),random.randint(1,5)

add =(left+right)
sub =(left-right)
mul =(left*right)

StrAdd =("+")
StrSub =("-")
StrMul =("*")
StrOp=StrAdd,StrSub,StrMul
StrRandOp=random.choice(StrOp)
print("\n\nwhat is\n",left,"",StrRandOp,"",right,)

given = float(input("\nAnswer here: "))
while number < 10:
    if given == add:
        print("you got the answer right,well done")
        correct=correct+1
        number=number+1
    elif given == sub:
        print("you got the answer right,well done")
        correct=correct+1
        number=number+1
    elif given == mul:
        print("you got the answer right,well done")
        correct=correct+1
        number=number+1
    else:
         print("wrong")
print("""Welldone you have answered all 10 questions and managed to get
correct:""", correct)
input("press enter to exit")

This is what you want: 这就是你想要的:

import random


correct,number,left,right = 0,0,random.randint(6,10),random.randint(1,5)

add =(left+right)
sub =(left-right)
mul =(left*right)

StrAdd =("+")
StrSub =("-")
StrMul =("*")
StrOp=StrAdd,StrSub,StrMul
StrRandOp=random.choice(StrOp)
print("\n\nwhat is\n",left,"",StrRandOp,"",right,)

given = float(input("\nAnswer here: "))
while number < 10:
    if given == add:
        print("you got the answer right,well done")
        correct=correct+1
    elif given == sub:
        print("you got the answer right,well done")
        correct=correct+1
    elif given == mul:
        print("you got the answer right,well done")
        correct=correct+1
    else:
         print("wrong")
    number = number + 1 
print("""Welldone you have answered all 10 questions and managed to get
correct:""", correct)
    input("press enter to exit")

When you want to iterate for a specific number of times it's better to use the for loop: 当您要迭代特定次数时,最好使用for循环:

for number in range(0,10,1):
    print "sth"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM