python中Hungman游戏代码显示不同的输出



我尝试设计一款饿人游戏。

Hangman是一个猜字游戏。要猜的单词由一排破折号表示,代表单词的每个字母。如果猜测玩家(在这种情况下是你的模型)建议在单词中出现一个字母,那么字母就会显示在所有正确的位置上。如果建议的字母没有出现在单词中,那么它将被标记为错误的尝试。猜字者最多只能猜错8次。

代码块是


import random
import string
def get_valid_word(words):
#randomly choice words from list
word = random.choice(words)
# the loop iterate until we get the word that does not have - or space on it.
return word
def hungman():
words = ["dog", "frog", "hog", "slob", "bacon", "avacado"]
word = get_valid_word(words)
#keep track what already been guessed in the word
word_letters = set(word)
alphabet = set(string.ascii_uppercase)
#what the user had guessed
used_letters = set()
#getting user input
while len(word_letters) > 0:
#letters used
print("You have used these letters: ")
print(", ".join(used_letters))
#what the current word is
word_list = [letter if letter in used_letters else '-'  for letter in word]
print("Current word: ")
print(", ".join(word_list))
user_letter = str(input("Guess a letter : ")).upper()
'''
If this is the valid character in the alphabet that user has not use yet
then user going to add this to his or her used letter list
'''
if user_letter in alphabet - used_letters:
used_letters.add(user_letter)
# if the letter that user just guessed in the word, it would be removed from word_letters
if user_letter in word_letters:
word_letters.remove(user_letter)
print(" ")
elif user_letter in used_letters:
print("Sorry! this seems to be already used charater. Try again: ")
else:
print("Invalid input. Try again ")
hungman()

没有得到正确的输出

得到的输出:

Guess a letter : S
S
-
Guess a letter : a
SYou have use this letters: ', ' A
-
Guess a letter : d
DYou have use this letters: ', ' SYou have use this letters: ', ' A
-
Guess a letter : u
DYou have use this letters: ', ' UYou have use this letters: ', ' SYou have use this letters: ', ' A

print("Current word: ', ' ".join(word_list))这行在我的输出中不打印任何东西。

实际输出:

guess a letter: e
You have used this letters : E
Current word : -----E
guess a letter: a
You have used this letters : E A
Current word : ---A-E
guess a letter: i
You have used this letters : I E A
Current word : ---A-E

我不明白我的代码出了什么问题。

这段代码有点难以理解,但我把它缩减了一点,并去掉了一些依赖项。

下面是一个可以工作的代码版本。我敢肯定它没有考虑到一些可能出错的事情,比如输入双字母。示例:"ad", "ba"等。


import random
import string
# Some words have spaces and - in the middle of the words. So we need to find out valid word
def get_valid_word(words):
#randomly choice words from list
word = random.choice(words)
# the loop iterate until we get the word that does not have - or space on it.
return word
def hungman():
words = ["dog", "frog", "hog", "slob", "bacon", "avacado"]
word = get_valid_word(words)
#keep track what already been guessed in the word
word_letters = set(word)
alphabet = set(string.ascii_uppercase)
#what the user had guessed
used_letters = set()
#getting user input
while len(used_letters) < 8:
#letters used
print("You have used these letters: ")
print(", ".join(used_letters))
#what the current word is
word_list = [letter if letter in used_letters else '-'  for letter in word]
print("Current word: ")
print(", ".join(word_list))
user_letter = str(input("Guess a letter : ")).lower()
'''
If this is the valid character in the alphabet that user has not use yet
then user going to add this to his or her used letter list
'''
if user_letter in word_letters:
used_letters.add(user_letter)
if len(used_letters) == len(word_letters):
print("you win")


hungman()

代码中出错的地方太多了,很难简明地总结出所有错误。但是,如果您仔细检查您所拥有的并将其与diff工具中的内容进行比较,您将发现一些使代码无法正常工作的东西。

另外,您可能会对以下内容感兴趣:https://github.com/dgwaldo/pytxtgames/blob/main/hangman.py

这是一个Python Hangman游戏,我为一些学生编写了一个有趣的示例应用程序。你可以在那里找到更多的想法……

最新更新