【问题标题】:Why doesn't my hangman game work the way it's supposed to为什么我的刽子手游戏没有按应有的方式运行
【发布时间】:2019-07-03 01:55:56
【问题描述】:
import random
import sys

word_list = ['zebra',  'memory']

guess_word = []
secret_word = random.choice(word_list)
lenght_word = len(secret_word)
alphabet = 'abcdefghijklmnopqrstuvwxyz'
letter_storage = []

def play_func():
    print('Great moment to play HANGMAN!')

    while True:
        game_choice = input('Do you want to play? ').lower()

        if game_choice == 'yes' or 'y':
            break
        elif game_choice == 'no' or 'n':
            sys.exit('That is a shame! BYE!')
        else:
            print('Please answer only Yes/y or No/n')
            continue

play_func()

def change():
    for character in secret_word:
        guess_word.append("-")

    print('The word you need to guess has', lenght_word, 'characters')
    print('Be aware that you can only enter 1 letter from a-z')
    print('If you want to exit type quit')
    print(guess_word)

def guessing():
    guess_taken = 0
    while  guess_taken < 8:
        guess = input('Pick a letter: ').lower()

        if guess == 'quit':
            break
        elif not guess in alphabet:
            print('Enter a letter from a-z')
        elif guess in letter_storage:
            print('You have already guessed that letter')
        else:
            letter_storage.append()
            if guess in secret_word:
                print('You guessed correctly!')
                for x in range(0, lenght_word):

我认为问题出在: 除了从没有 如果 secret_word[x] == 猜测: 猜测字[x] == 猜测 打印(guess_word) 如果在 guess_word 中不是“-”: print('你赢了!') 休息 别的: print('字母不在单词中。再试一次!') 猜测+= 1 如果guess_taken == 8: print('你输了,密码是:', secret_word)

change()
guessing()
print('Game Over!')


if secret_word[x] == guess:
    guess_word[x] == guess

I think the problem is on these lines of code because they don't actually replace guess_word

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    希望这能让您走上正轨。我刚刚创建了一个新方法/函数来包含所有其他函数,并修复了 append 语句。祝你好运!

    import random
    import sys
    
    word_list = ['zebra',  'memory']
    
    guess_word = []
    secret_word = random.choice(word_list)
    lenght_word = len(secret_word)
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    letter_storage = []
    
    
    
    def main():
        play_func()
        change()
        guessing()
    
    
    def play_func():
        print('Great moment to play HANGMAN!')
    
        while True:
            game_choice = input('Do you want to play? ').lower()
    
            if game_choice == 'yes' or 'y':
                break
            elif game_choice == 'no' or 'n':
                sys.exit('That is a shame! BYE!')
            else:
                print('Please answer only Yes/y or No/n')
                continue
    
    
    def change():
        for character in secret_word:
            guess_word.append("-")
    
        print('The word you need to guess has', lenght_word, 'characters')
        print('Be aware that you can only enter 1 letter from a-z')
        print('If you want to exit type quit')
        print(guess_word)
    
    def guessing():
        guess_taken = 0
        while  guess_taken < 8:
            guess = input('Pick a letter: ').lower()
    
            if guess == 'quit':
                break
            elif not guess in alphabet:
                print('Enter a letter from a-z')
            elif guess in letter_storage:
                print('You have already guessed that letter')
            else:
                letter_storage.append(guess)
                if guess in secret_word:
                    print('You guessed correctly!')
                    for x in range(0, lenght_word):
                        print(x)
    
    main()        
    

    【讨论】:

    • 代码写得更好,但还是不能正常工作
    • 它实际上并没有完成游戏,但它确实使您编写的代码运行时没有错误。我的印象是你仍然必须在猜测方法中的for循环中完成游戏。
    猜你喜欢
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 2021-02-16
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多