如何结束我的刽子手游戏,以及如何告诉用户 - 恭喜

时间:2017-07-21 19:40:45

标签: python

好的,三个问题。

1-我正在使用' thing'和if语句来检查用户的输入。因此,如果他/她猜到的单词是完整的,那么在所有if语句中都会显示为真,并显示最终的恭喜!但它没有按预期工作。

2-为什么我的elif声明最终没有工作?当计时器达到0时。这是零猜测,所以游戏结束了。但它没有用!

3-如何跟踪他/她用完的字母。它现在的工作方式,如果他/她在第一次猜测时猜到了字母表,那么第二次猜测字母表是不可见的。但在第3次猜测它会回来。但我想要的是保持这个字母不可见的下一个猜测。而不是它是最后一个猜测的字母表消失。我希望这很清楚。

如果有人能帮助1和2,我会非常高兴!合十礼。

def get_available_letters(letters_guessed):

    a = string.ascii_lowercase
    for char1 in a:
        if char1 in letters_guessed:
           a = a.replace(char1,"")


    return(a)


def hangman(secret_word):
    timer = 6
    warning = 3
    secret_word = 'geranimo'
    alph = string.ascii_lowercase
    guessed_letters = len(secret_word) * ['_']


    if timer >= 1:
        while timer >= 1:
            print('You have', timer, 'guesses left.')
            letters_guessed = str(input('Guess a letter: '))


            if letters_guessed not in alph and warning >= 1:
                print('Only alphabets allowed!!!')
                print('You have',warning, 'warnings')
                warning -= 1

            if letters_guessed not in alph and warning <= 0:
                    timer -= 1

            for position, letter in enumerate(secret_word):
                if letter == letters_guessed:
                    guessed_letters[position] = letter
                    print(' '.join(guessed_letters))

            cons =['b','c','d','f','h','j','k','l','p','q','s','t','v','w','x','y','z']
            for thing in cons:
                if letters_guessed == thing: 
                    print('Wrong Consonant!')
                    timer -= 1
            vow =['u']
            for thing in vow:
                if letters_guessed == thing: 
                    print('Wrong Vowel!')
                    timer -= 2

            for thing in guessed_letters:
                if thing == 'g':
                    if thing == 'e':
                        if thing == 'r':
                            if thing == 'a':
                                if thing == 'n':
                                    if thing == 'i':
                                        if thing == 'm':
                                            if thing == 'o':
                                                print ('Congrats game won!')
                                                break
                else:
                    break



            alpha = get_available_letters(letters_guessed)
            for position, letter in enumerate(alpha):
                if letter == letters_guessed: 
                    alpha.delete(position)


            print('Available letters: ' + alpha)


    elif timer == 0:
        print("Game over douchebag")
        quit()

1 个答案:

答案 0 :(得分:0)

这是一个链接,可以帮助您了解如何以正确的方式http://www.pythonforbeginners.com/code-snippets-source-code/game-hangman