我怎么能关闭一个while循环,在我回答10个问题后进行的测验?

时间:2014-12-10 09:16:43

标签: python random while-loop

while True:
    if play.lower() == "yes":
        print("great, here we go!")

        first = random.randint(1, 10)
        second = random.randint(1, 20)

        questionA=input("what is" + str(first) + "+" + str(second))

        userAnswer = first + second

        if userAnswer == userAnswer:
            print("well done")


    elif play.lower() == "no":
        print("ok goodbye")

1 个答案:

答案 0 :(得分:0)

您可以通过使用某种变量来解决这个问题,该变量可以跟踪问题的数量。例如:在下面的代码片段中,检查noOfQuestions是否等于或小于10,如果是,则执行while循环,否则不执行。

<强>代码:

noOfQuestions = 1
while noOfQuestions<=10: #<-- check if noOfQuestions is less than or equal to 10
    print noOfQuestions
    noOfQuestions += 1 #<--- update the noOfQuestions variable
else:
    print ("noOfQuestions is %d.")%noOfQuestions