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)
答案 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()
在评论中给出了新信息。