无法理解如何在python中解决hangman显示问题
我很难弄清楚如何在我的python刽子手游戏中显示刽子手。我当前的代码在display_word函数下的工作方式是,它将为任何一个我的hangman单词显示一个“-”。当我猜一个字母正确时,它将根据字符串中字母的数量缩短“-”显示。我想在这个显示中添加那个字母到显示中,并且仍然保留我没有猜到的字母的“-”。谁能解决这个问题无法理解如何在python中解决hangman显示问题,python,Python,我很难弄清楚如何在我的python刽子手游戏中显示刽子手。我当前的代码在display_word函数下的工作方式是,它将为任何一个我的hangman单词显示一个“-”。当我猜一个字母正确时,它将根据字符串中字母的数量缩短“-”显示。我想在这个显示中添加那个字母到显示中,并且仍然保留我没有猜到的字母的“-”。谁能解决这个问题 from random import choice print('Welcome to Hangman!!!, Guess the secret word
from random import choice
print('Welcome to Hangman!!!, Guess the secret word ')
org_word = choice(['wallee'])
word = set(org_word)
already_guessed = ''
correct_letters = ''
chances = 2
def hang_man():
while not check_win():
guess()
def guess():
global chances
global correct_letters
global already_guessed
if chances > 0:
display_word()
user_guess = input('\nWhat is your guess at the secret word? ')
#check for valid input
if len(user_guess) > 1 or not user_guess.isalpha():
print('you entered an invalid input and you lost a guess')
chances -= 1
check_loss()
#check if letter was already guessed
elif user_guess in already_guessed:
print('you already entered that letter and you lost a guess')
chances -=1
check_loss()
#incorrect guess
elif user_guess not in word:
print('\nThat guess was not in the word and you lost a guess')
chances -= 1
already_guessed += user_guess
check_loss()
#correct guess
elif len(user_guess) == 1 and user_guess in word:
print(f'\nGreat, your guess of {user_guess} was in the word')
correct_letters += user_guess
word.remove(user_guess)
#need help here
def display_word():
global correct_letters
stringed_word = (str(org_word)[:])
for char in stringed_word:
if char not in correct_letters:
print(char.replace(char, '-'), end='')
#need help here
def check_win():
if len(word) == 0:
print(f'Congrats, you guessed the correct word {org_word}')
return True
def check_loss():
if chances == 0:
print('Sorry you are out of guesses, You Lost :(')
else:
print(f'you now have {chances} guesses left')
hang_man()
我认为你让这件事变得更加困难了。对于每个字母,如果是猜测的,则打印字母,否则打印破折号。无需更换
def display_word():
for char in org_word:
if char in correct_letters:
print(char, end=' ')
else:
print('-', end=' ')
伟大的解决方案,蒂姆!!