Python 刽子手游戏猜字母

Python 刽子手游戏猜字母,python,python-3.x,function,Python,Python 3.x,Function,那就玩刽子手游戏。 我猜我的guessWord(word)函数中有一个错误,因为它工作不正常,我不明白为什么? readDictionary的文件包含游戏的单词行。但是在主代码中也可以使用单词 电流输出: Welcome to the hangman game. You will be guessing words, one letter at a time Guess a letter a Would you like to guess a new word? Y/N: y Guess a l

那就玩刽子手游戏。 我猜我的
guessWord(word)
函数中有一个错误,因为它工作不正常,我不明白为什么?
readDictionary
的文件包含游戏的单词行。但是在主代码
中也可以使用单词

电流输出:

Welcome to the hangman game.
You will be guessing words, one letter at a time
Guess a letter
a
Would you like to guess a new word? Y/N: y
Guess a letter
h
Would you like to guess a new word? Y/N: g
You guessed 2 words out of 2
期望输出:

Your guess so far: -------
Guess a letter from the secret word: a 
Good guess;
Your guess so far: -A----A
Guess a letter from the secret word: e 
Wrong guess
--------
|

Your guess so far: -A----A
Guess a letter from the secret word: s 
Wrong guess
--------
| 
O
#and so on…


以下是要执行的参数: readDictionary()读取附带的文件“dictionary.txt”,并返回文件中出现的所有单词的列表

•guessWord(word),运行用户界面以猜测作为参数传递的单词,如上所述。每当玩家输入错误的猜测时,guessWord()就会使用适当的参数调用hangmanSketch()

请注意,即使中的所有单词都是大写字母,玩家也应该能够以小写字母输入猜测

如果玩家在做出8次错误猜测之前猜到了整个单词,则函数返回True。如果玩家猜错了8次,并且没有猜到整个单词,则函数返回False

下面是整个应用程序运行示例的摘录,显示了成功和失败猜测单词的界面


代码:

来自随机导入选择
def readDictionary():
文件=打开(“dictionary.txt”、“r”)
lines=file.readlines()
返回列表(行)
def hangmanSketch(n):

如果n我建议你用一个
while
在单词之间迭代,用一个
while
在玩家的猜测中迭代:

import random

WORDS = ['abandon', 'inquiring', 'lacrosse', 'reinitialised']


def guessed_word(word, guessed_letters):
    """ Build guessed word. E.g.: "--c--r" """
    result = ''
    for letter in word:
        if letter in guessed_letters:
            result += letter
        else:
            result += '-'
    return result


print('Welcome to the hangman game.')
print('You will be guessing words, one letter at a time')
play = 'y'
while play == 'y':
    word = random.choice(WORDS)
    guessed_letters = []
    hp = 8
    while True:
        print(f'Your guess so far: {guessed_word(word, guessed_letters)}')
        letter = input('Guess a letter from the secret word: ')
        if letter in list(word):
            print('Good guess')
            guessed_letters.append(letter)
        else:
            print('Wrong guess')
            hp -= 1

        if hp == 0:
            print('You loose!')
            break
        elif len(guessed_letters) == len(set(word)):
            print(f'You win! The word is: {word}')
            break

    play = input('Would you like to guess a new word? y/n: ')
输出:

Welcome to the hangman game.
You will be guessing words, one letter at a time
Your guess so far: --------
Guess a letter from the secret word: a
Good guess
Your guess so far: -a------
Guess a letter from the secret word: b
Wrong guess
Your guess so far: -a------
Guess a letter from the secret word: c
Good guess
Your guess so far: -ac-----
Welcome to the hangman game.
You will be guessing words, one letter at a time
Your guess so far: --------
Guess a letter from the secret word: a
Good guess
Your guess so far: -a------
Guess a letter from the secret word: b
Wrong guess
Your guess so far: -a------
Guess a letter from the secret word: c
Good guess
Your guess so far: -ac-----