如何创建一个将结束程序的函数?



我有困难创建计数器(这是errorCount)为我的while循环语句。我希望我的计数器的功能是,如果用户回答错误的问题5次,程序将终止。此外,我有3个问题要问用户,我想把所有的errorCounts加起来,这样如果它达到5,程序就会终止。

例如:如果用户错误地回答了问题1两次,那么errorCount将是2。如果用户三次错误地回答问题2,那么程序将被终止。然而,该程序允许用户对每个问题犯5个错误。
# Level 5:
print("You have the jewel in your possession, and defeated Joker at his own game")
print("You now hold the precious jewel in your hands, but it's not over, you must leave the maze!")
print("*You must now choose 'Right', 'Left', or 'Straight' as you exit the maze. Keep trying until you find your path.*")
# put an error limit
# space everything out to make it look more neat
# Make sure you can fail the level
**errorCount = 0**
position = 0
while True:
answer1 = input("Choose either Right, Left, Straight: ")


try:
if answer1.lower() == "right":
print("You have chosen the correct path, now you proceed to the next step!")
print()
break
*     
if errorCount == 5:
print("you made too many mistakes and got captured, you have to restart")*

elif answer1.lower() == "left":
print("You see a boulder blocking your path which forces you to go back.")
errorCount = 1 + errorCount

elif answer1.lower()  == "straight":
print("On your way to the next stage you are exposed to a toxic gas that forces you to go back .")
errorCount = 1 + errorCount

else:
print("Wrong input. Please try again..")
errorCount = 1 + errorCount
except Exception:
print("Wrong input. Please try again..")
# if errors >= 5:
while True:
answer1 = input("Choose either Right, Left, Straight: ")

if errorCount == 5:
print("you made too many mistakes and got captured, you have to restart")

try:
if answer1.lower() == "straight":
print("You have chosen the correct path, now you proceed to the next step!")
break


elif answer1.lower() == "left":
print("You chose the wrong path, go back")
errorCount = 1 + errorCount

elif answer1.lower() == "right":
print("You chose the wrong path, go back")
errorCount = 1 + errorCount

else:
print("Wrong input. Please try again..")
errorCount = 1 + errorCount

except Exception:
print("Wrong input. Please try again..")
print("You are now on the third stage, you notice a screen that is asking you a riddle")
while True:
riddle1 = input("What gets wet when drying? ")

if errorCount == 5:
print("you made too many mistakes and got captured, you have to restart")

try:
if riddle1.lower() == "towel":
print("You have chosen the correct answer")
print("The giant stone blocking the entrance of the maze opens, and the outside lights shine through..")
break

else:
print("Incorrect! Try again..")
errorCount = 1 + errorCount
print("Heres a hint: You use it after taking a shower...")

except Exception:
print("Incorrect! Try again..")
errorCount = 1 + errorCount



I do not know how to fix this issue 

你把代码弄得太复杂了。

questions = ["You have the jewel in your possession, and defeated Joker at his own game",
"You now hold the precious jewel in your hands, but it's not over, you must leave the maze!",
"*You must now choose 'Right', 'Left', or 'Straight' as you exit the maze. Keep trying until you find your path.*"]
error_count = 0
for id, query in enumerate(questions):
if id == 0:
# call method for query 1
# write your while error_count < 5 loop inside the method
# return error_count to check.
pass
elif id == 1:
pass
elif id == 2:
pass

if error_count > 5:
break

如果使用try/except,也可以引发User警告。

except UserWarning:
if error_count > 5:
print("you made too many mistakes and got captured, you have to restart")
break

您只是忘记将errorCount条件放在最后一个循环中,并且最好将其放在用户输入之前,这样它就不会问问题6次而不是您想要的5次。最后,有必要在条件语句的末尾添加一个断点,这样就不会出现无限循环

# Level 5:
print("You have the jewel in your possession, and defeated Joker at his own game")
print("You now hold the precious jewel in your hands, but it's not over, you must leave the maze!")
print("*You must now choose 'Right', 'Left', or 'Straight' as you exit the maze. Keep trying until you find your path.*")
# put an error limit
# space everything out to make it look more neat
# Make sure you can fail the level
errorCount = 0
position = 0
while True:
if errorCount == 5:
print("you made too many mistakes and got captured, you have to restart")
break

answer1 = input("Choose either Right, Left, Straight: ")

try:
if answer1.lower() == "right":
print("You have chosen the correct path, now you proceed to the next step!")
print()
break

elif answer1.lower() == "left":
print("You see a boulder blocking your path which forces you to go back.")
errorCount = 1 + errorCount

elif answer1.lower()  == "straight":
print("On your way to the next stage you are exposed to a toxic gas that forces you to go back .")
errorCount = 1 + errorCount

else:
print("Wrong input. Please try again..")
errorCount = 1 + errorCount
except Exception:
print("Wrong input. Please try again..")
# if errors >= 5:
while True:
if errorCount == 5:
print("you made too many mistakes and got captured, you have to restart")
break

answer1 = input("Choose either Right, Left, Straight: ")

try:
if answer1.lower() == "straight":
print("You have chosen the correct path, now you proceed to the next step!")
break

elif answer1.lower() == "left":
print("You chose the wrong path, go back")
errorCount = 1 + errorCount

elif answer1.lower() == "right":
print("You chose the wrong path, go back")
errorCount = 1 + errorCount

else:
print("Wrong input. Please try again..")
errorCount = 1 + errorCount

except Exception:
print("Wrong input. Please try again..")
print("You are now on the third stage, you notice a screen that is asking you a riddle")
while True:
if errorCount == 5:
print("you made too many mistakes and got captured, you have to restart")
break

riddle1 = input("What gets wet when drying? ")

if errorCount == 5:
print("you made too many mistakes and got captured, you have to restart")
try:
if riddle1.lower() == "towel":
print("You have chosen the correct answer")
print("The giant stone blocking the entrance of the maze opens, and the outside lights shine through..")
break

else:
print("Incorrect! Try again..")
errorCount = 1 + errorCount
print("Heres a hint: You use it after taking a shower...")

except Exception:
print("Incorrect! Try again..")
errorCount = 1 + errorCount

我建议不要使用:

errorcount = 1 + errorcount

最好使用:

errorcount += 1

最新更新