进入下一个输入的问题 - Python中的猜谜游戏



我在进入下一个问题时遇到了问题,我的猜谜游戏。

我该怎么做?我真的试过了,但我不知道。

def guessingGame():
    anwsers = ["Kononowicz", "Suchodolski"]
    questionTries = 3
    questionTriesCounter = 0
    while questionTriesCounter < questionTries:
         firstQuestion = input("What is the surname of famous president from Bialystok? ")
         if firstQuestion == anwsers[0]:
            secondQuestion = input("What is the nick")
         else:
            questionTriesCounter+= 1
         if questionTriesCounter == questionTries:
            print('game over.')

guessingGame()

你的代码看起来至少可以在python3中工作。输入一些打印行以查看代码的位置并告诉玩家正在发生的事情会有所帮助

def guessingGame():
anwsers = ["5", "1"]
questionTries = 3
questionTriesCounter = 0
while questionTriesCounter < questionTries:
     firstQuestion = input("What is 1 + 4 ")
     print(firstQuestion)
     if firstQuestion == anwsers[0]: 
        secondQuestion = input("what is 5 - 4")
        if secondQuestion == anwsers[1]:
            print("Game won.")
            break
     else:
        questionTriesCounter+= 1
        print (f'Wrong answer. You have used {questionTriesCounter} of {questionTries} tries')
     if questionTriesCounter == questionTries:
        print('game over.')

猜谜游戏((

游戏

的工作方式是这样的,并在控制台中打印出答案,以更好地帮助查看游戏当前处于哪个点

您可以将问题放入 while 循环中,并使用函数通常处理如下问题:

def question(question, questionTries, answer, questionTriesCounter):
    guess = ""   
    while (questionTriesCounter > questionTries and guess != answer):
        guess = input(question)
        questionTries += 1
    return [guess == answer, questionTries]

def guessingGame():
    anwsers = ["Kononowicz", "Suchodolski"]
    questionTriesCounter = 3
    questionTries = 0
    firstQuestion = question("What is the surname of famous president from Bialystok? ",
                             questionTries,
                             anwsers[0],
                             questionTriesCounter)
    if firstQuestion[0] == True:
        secondQuestion = question("What is the nick? ",
                                  questionTries,
                                  anwsers[1],
                                  questionTriesCounter)
        if secondQuestion[0] == True:
            print("Congrats you won!")        
        else:
            print('game over. maximum number of trials reached!')    
    else:
        print('game over. maximum number of trials reached!')

guessingGame()

最新更新