简体   繁体   English

刽子手游戏字母猜测

[英]Hangman game letter guessing

that plays hangman game.玩刽子手游戏。 I guess a have an error in my guessWord(word) function, because its not working properly, and I'm not getting why?我猜我的guessWord(word) function 中有一个错误,因为它不能正常工作,我不明白为什么? the file for readDictionary contains rows of words for the game. readDictionary的文件包含游戏的单词行。 but in the main code words can also be used.但在主码words中也可以使用。

current output:当前 output:

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

desired output:所需的 output:

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…


here are the parameters to do: readDictionary() which reads the accompanying file “dictionary.txt” and returns a list of all the words appearing in the file.以下是要执行的参数: readDictionary() 读取随附文件“dictionary.txt”并返回文件中出现的所有单词的列表。

• guessWord(word) which runs the user interface for guessing the word passed as argument, as described above. •guessWord(word),它运行用户界面来猜测作为参数传递的单词,如上所述。 guessWord() calls hangmanSketch() with the appropriate argument, whenever a wrong guess is entered by the player.每当玩家输入错误的猜测时,guessWord() 就会使用适当的参数调用 hangmanSketch()。

Note that, even though all the words in the are in all-capital letters, the player should be able to enter the guesses in lower case.请注意,即使 中的所有单词都是大写字母,玩家也应该能够输入小写的猜测。

The function returns True, if the player manages to guess the whole word before making 8 wrong guesses. function 返回 True,如果玩家在猜错 8 次之前猜出了整个单词。 The function returns False, if the player makes 8 wrong guesses and does not guess the whole word. function 返回 False,如果玩家猜错 8 次并且没有猜出整个单词。

Below is an extract of a sample run of the whole application, showing the interface for successfully and unsuccessfully guessing words.下面是整个应用程序运行示例的摘录,显示了猜词成功和失败的界面。


code:代码:

from random import choice


def readDictionary():
    file = open("dictionary.txt", "r")
    lines = file.readlines()
    return list(lines)

def hangmanSketch(n):
    if n <= 0:
        pic = '''  --------\n'''
        return pic
    pic = hangmanSketch(n-1)
    if n == 1:
        pic += '''  |\n'''
    elif n == 2:
        pic += '''  O\n'''
    elif n == 3:
        pic += '''_/'''
    elif n == 4:
        pic += '''|'''
    elif n == 5:
        pic += '''\_ \n'''
    elif n == 6:
        pic += '''  |\n'''
    elif n == 7:
        pic += '''_/'''
    elif n == 8:
        pic += ''' \_ \n'''
    return pic


def guessWord(word):
    while True:
        print("Guess a letter")
        userGuess = input()
        userGuess = userGuess.lower()
        if len(userGuess) != 1:
            print("Please enter a single letter")
        elif userGuess in word:
            print("letter already guessed, try another")
        else:
            return userGuess



def main():
    print("Welcome to the hangman game.")
    print("You will be guessing words, one letter at a time")
    words = readDictionary()
    words = ['ABANDON', 'INQUIRING', 'LACROSSE', 'REINITIALISED'] # use this list if you don't manage to implement readDictionary()
    nAttemptedWords = 0
    nGuessedWords = 0
    play = "Y"
    while play == "Y":
        secretWord = choice(words)  # random choice of one word from the list of words
        nAttemptedWords += 1
        if guessWord(secretWord):
            nGuessedWords += 1
        play = input("Would you like to guess a new word? Y/N: ")
        play = play.upper()
    print("You guessed", nGuessedWords, "words out of", nAttemptedWords)

if __name__ == "__main__":
    main()

I suggest you having one while for iterating between words and inner while for iterating through player's guesses:我建议你有一段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: ')

Output: Output:

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-----

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM