我试图在python上问一个问题,这样如果这个人做对了,他们就可以进入下一个问题了。如果他们弄错了,那么在测验进入下一个问题之前,他们有3次左右的尝试。我以为我用下面的程序解决了它,但是这只是要求用户做出另一个选择,即使他们弄错了。如果用户得到正确的话,我如何进入下一个问题,又为那些错误的人提供了另一个机会?
score = 0
counter = 0
while counter<3:
answer = input("Make your choice >>>> ")
if answer == "c":
print("Correct!")
score += 1
else:
print("That is incorrect. Try again.")
counter = counter +1
print("The correct answer is C!")
print("Your current score is {0}".format(score)
答案 0 :(得分:2)
你陷入了困境。所以把
counter = 3
之后
score += 1
离开循环。
score = 0
counter = 0
while counter<3:
answer = input("Make your choice >>>> ")
if answer == "c":
print("Correct!")
score += 1
counter = 3
else:
print("That is incorrect. Try again.")
counter = counter +1
print("The correct answer is C!")
print("Your current score is {0}".format(score)
答案 1 :(得分:1)
你被困在循环中,一种更清晰的解决方法是使用函数break,如下所示:
score = 0
counter = 0
while counter < 3:
answer = input("Make your choice >>>> ")
if answer == "c":
print ("Correct!")
score += 1
break
else:
print("That is incorrect. Try Again")
counter += 1
print("The correct answer is C!")
print("Your current score is {" + str(score) + "}")
我想强调一下原始代码的一些内容。
1- Python区分大小写,只要您以小写字母键入“c”,您提供给我们的代码就会有效。
2-我编辑它的最后一行,以便正确打印得分。
有关控制流程和函数中断的进一步阅读,请尝试python docs:https://docs.python.org/2/tutorial/controlflow.html