我正在为自己的任务开发游戏,并且卡在某个部件上,如果用户连续5次正确回答,那么我需要显示一条消息,例如“太棒了,您是这个大师”。像这样的东西:
if correct_guesses == 5 and incorrect_guesses == 0 :
print("""CONGRATULATIONS! You have figured it all out Great work mate!!""")
我已经设置了可记录正确和错误猜测答案的字符串
现在,如果正确的猜测等于5,我的代码将执行代码并打印,但是如果我得到一个错误的猜测,它将不会显示,有什么帮助吗?
每当答案错误时,您都需要重置计数器。结构如下所示:
game_is_running = True # we run the game as long as this is True
counter = 0
while game_is_running:
was_correct = ask_question() # ask_question() is a function that prints the question and check if it was answered correctly. It correct, return True otherwise return False
if was_correct: # if the answer was correct, we increase the counter
counter += 1
else: # if it was false, we reset it to 0 (we need 5 IN A ROW, right?)
counter = 0
if counter == 5: # if we had 5 correct answers in a row, we print and then the game is finished
print("CONGRATULATIONS! You have figured it all out! Great work mate!!")
game_is_running = False
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句