如果条件和while循环不能正常工作

时间:2018-04-04 13:43:36

标签: python python-2.7

如果条件没有停止游戏,当我做3次以上的错误时游戏应该停止,但实际上它会继续下一级,你能告诉我我的问题是什么吗?我也做了while循环,但后来它没有打印最后2行,我卡住了,我尝试了几个组合和方法,但idk做什么。感谢您的帮助。 谢谢。

import random
equations=("+","-","*","/")
print "Welcome to the random math question game!"
n = 0
total_wrong_answers=0
while n < 5:
    n = n + 1
    points = 0
    print "level:" + str(n)
    q=0
    wrong_answers = 0
    if wrong_answers < 3:
    #if wrong_answers != 3:
    #while wrong_answers != 3:
    #while wrong_answers < 3:
        while q < 5:
            q=q+1
            signs=random.choice(equations[:n])
            if n<4:
                num1 = random.randint(0,10)
                num2 = random.randint(0,10)
            else:
                num1 = random.randint(0, 100)
                num2 = random.randint(0, 100)
            result=eval(str(num1)+signs+str(num2))
            print (str(num1)+signs+str(num2)),"guess the result"
            answer=(input("answer:"))
            if answer==result:
                points=points+1
                print str(result) + " Right answer.","Your points:"+str(points)
                print points * "*"
            else:
                points = points
                print "Wrong answer.","Your points:"+str(points)
                print points*"*"
                total_wrong_answers=total_wrong_answers+1
                wrong_answers=wrong_answers+1
    else:
        print "the game has finished, total wrong answers:" + str(total_wrong_answers)
        print "wrong answers in this level:" + str(wrong_answers)

3 个答案:

答案 0 :(得分:1)

您在wrong_answers声明之前将if wrong_answers < 3设置为零。显然,wrong_answer总是小于3,因为它为零你必须移动这一行

wrong_answers = 0

到其他地方并改变逻辑,因为仅靠这一点是不够的。

答案 1 :(得分:0)

如果我理解你的问题,你希望游戏一旦完成3个错误就结束。

考虑将条件添加到while q < 5:部分:

while (q < 5)and(wrong_answers<3):

答案 2 :(得分:0)

我认为你的错误地点在你想要的地方,你仍然需要摆脱循环或退出。像这样:

import random

equations = ("+", "-", "*", "/")
print "Welcome to the random math question game!"
n = 0
total_wrong_answers = 0
wrong_answers = 0
while n < 5:
    n = n + 1
    points = 0
    print "level:" + str(n)
    q = 0

    if wrong_answers < 3:
        wrong_answers = 0
        while q < 5:
            q = q + 1
            signs = random.choice(equations[:n])
            if n < 4:
                num1 = random.randint(0, 10)
                num2 = random.randint(0, 10)
            else:
                num1 = random.randint(0, 100)
                num2 = random.randint(0, 100)
            result = eval(str(num1) + signs + str(num2))
            print (str(num1) + signs + str(num2)), "guess the result"
            answer = (input("answer:"))
            if answer == result:
                points = points + 1
                print str(result) + " Right answer.", "Your points:" + str(points)
                print points * "*"
            else:
                points = points
                print "Wrong answer.", "Your points:" + str(points)
                print points * "*"
                total_wrong_answers = total_wrong_answers + 1
                wrong_answers = wrong_answers + 1
    else:
        print "the game has finished, total wrong answers:" + str(total_wrong_answers)
        print "wrong answers in this level:" + str(wrong_answers)
        exit()

在评论中给出了新信息。