Python刽子手游戏,不起作用

时间:2016-11-11 01:34:00

标签: python

我正在尝试用Python开发一个Hangman游戏,我很难弄清楚为什么程序没有记住正确猜到的字母。

例如,如果输入的字母在单词" hello"中,python会识别匹配但是当它提示输入另一个字母时,它会忘记updt_list(列表中有输入的字母)。

如果有人愿意看看我会非常感激。

import time


def pick_a_word():

    word = "hello"
    return word

def guess_a_letter():
    aletter = input("Please input one letter at a time:\n")
    no_letters= len(aletter)
    if no_letters > 1:
        print("You inputed more than one letter! Don't forget- one at a time.\nLets try again..")
        time.sleep(2)
        return guess_a_letter()
    else:
        return aletter


def blankLIST(aword):
    blankList = len(aword) * "_"
    return list(blankList)

def check(aletter,aword,blankLIST):

    updt_list= blankLIST(aword)

    which_letter= aword.index(aletter)

    updt_list[which_letter] = aletter

    return updt_list


def engine():

    guess=guess_a_letter()
    wordtoguess=pick_a_word()
    if guess in wordtoguess:
        print("\n")
        print("The letter is in the word")
        print(check(guess,wordtoguess,blankLIST))

        time.sleep(1)
    else:
        print("\n")
        print("The letter is not in the word")

        time.sleep(1)




def repeat():
    x = 0
    while x <= 5:
        lifes= 6- x
        print("You have",lifes," attempts left")

        engine()

        x = x

repeat()

1 个答案:

答案 0 :(得分:1)

请注意,这是非常不优雅的,如果你不断猜测这封信,那就会有问题。&#39; l&#39;在这种情况下...我没有更多的时间花在它上面,但它应该足以让你向前推进一点。它记得现在玩家的猜测,用户可以赢得比赛。我道歉......

    import time


    def pick_a_word():

        word = "hello"
        return word

    def guess_a_letter():
        #aletter = input("Please input one letter at a time:\n")
        aletter = 'l'
        no_letters= len(aletter)
        if no_letters > 1:
            print("You inputed more than one letter! Don't forget- one at a time.\nLets try again..")
            time.sleep(2)
            return guess_a_letter()
        else:
            return aletter


    def buildWordLIST(aword):
        blankList = len(aword) * "_"
        return list(blankList)

    def check(aletter,aword,updt_list, wordIndex=0):

        which_letter= aword.index(aletter, wordIndex)

        if updt_list[which_letter] != "_" and ((wordIndex + 1) < len(aword)):
            wordIndex = which_letter + 1
            check(aletter,aword,updt_list, wordIndex)

        updt_list[which_letter] = aletter

        return updt_list


    def engine(mistakes, updt_list, correctGuesses):

        youWinFlag = False

        guess=guess_a_letter()
        wordtoguess=pick_a_word()
        if guess in wordtoguess:
            print("\n")
            print("The letter is in the word")
            updt_list = check(guess,wordtoguess,updt_list)
            print updt_list
            correctGuesses += 1
            if correctGuesses == len(wordtoguess):
                print("YOU GUESSED THE WORD!")
                youWinFlag = True


            time.sleep(1)
        else:
            print("\n")
            print("The letter is not in the word")
            mistakes += 1
            time.sleep(1)

        return mistakes, updt_list, correctGuesses, youWinFlag



    def repeat(updt_list):
        x = 0
        blankLIST = ''
        correctGuesses = 0
        while x <= 5:
            lives= 6- x
            print("You have",lives," attempts left")

            results = engine(x, updt_list, correctGuesses)
            x = results[0]
            updt_list = results[1]
            correctGuesses = results[2]
            youWinFlag = results[3]
            if youWinFlag:
                x = 6

    def initialize_game():
        wordtoguess=pick_a_word()
        updt_list = buildWordLIST(wordtoguess)
        return updt_list

    updt_list = initialize_game()
    repeat(updt_list)