简体   繁体   English

如果连续正确回答 x 次,则显示一条消息

[英]if answered x times correctly in a row, display a message

I'm working on a game for my assignment and I'm stuck on a part where I need to display a message like "Great you are a master at this" IF the user answers correctly 5 times in a row, what I did was something like this:我正在为我的作业开发一个游戏,我被困在一个部分,如果用户连续 5 次正确回答,我需要显示一条消息,比如“太棒了,你是这方面的大师”,我所做的是是这样的:

if correct_guesses == 5 and incorrect_guesses == 0 :
    print("""CONGRATULATIONS! You have figured it  all out Great work mate!!""")

I've already set up strings that record correct and incorrect guesses for the answers我已经设置了字符串来记录对答案的正确和错误猜测

Right now my code will execute the code and print if the correct guesses equal to 5 but if I get a single guess wrong it will not display, any help?现在我的代码将执行代码并在正确猜测等于 5 时打印,但如果我猜错了一个,它就不会显示,有什么帮助吗?

You need to reset the counter every time the answer is wrong.每次答案错误时,您都需要重置计数器。 The structure can look like this:结构可能如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM