Python 刽子手游戏失败了
我正在尝试制作这个刽子手游戏,但它不起作用。有人有什么建议来解决这个问题吗Python 刽子手游戏失败了,python,python-3.x,Python,Python 3.x,我正在尝试制作这个刽子手游戏,但它不起作用。有人有什么建议来解决这个问题吗 import random, os print("^^^^^^^^^^THIS IS HANGMAN^^^^^^^^^^") print("1. Play Game ") print("2. Quit Game ") choice = input("Please enter option 1 or 2") if choice == "1": words = ["handkerchief", "accommo
import random, os
print("^^^^^^^^^^THIS IS HANGMAN^^^^^^^^^^")
print("1. Play Game ")
print("2. Quit Game ")
choice = input("Please enter option 1 or 2")
if choice == "1":
words = ["handkerchief", "accommodate", "indict", "impetuous"]
word = random.choice(words)
guess = ['_'] * len(word)
guesses = 7
while '_' in guess and guesses > 0:
print(' '.join(guess))
character = input('Enter character: ')
if len(character) > 1:
print('Only enter one character.')
continue
if character not in word:
guesses -= 1
if guesses == 0:
print('You LOST!')
break
else:
print('You have only', guesses, 'chances left to win.')
else:
print(''.join(guess))
print('You WON, well done')
我想,当你说游戏不起作用时,你的意思是正确猜出的角色没有出现?那是因为你没有改变你的猜测变量。如果不每次更新它,它将始终只包含u个字符:
for i, c in enumerate(word):
if c == character:
guess[i] = character
因此,如果将其添加到代码中,其他内容不会发生任何更改,则整个游戏如下所示:
import random, os
print("^^^^^^^^^^THIS IS HANGMAN^^^^^^^^^^")
print("1. Play Game ")
print("2. Quit Game ")
choice = input("Please enter option 1 or 2")
if choice == "1":
words = ["handkerchief", "accommodate", "indict", "impetuous"]
word = random.choice(words)
guess = ['_'] * len(word)
guesses = 7
while '_' in guess and guesses > 0:
print(' '.join(guess))
character = input('Enter character: ')
if len(character) > 1:
print('Only enter one character.')
continue
if character not in word:
guesses -= 1
for i, c in enumerate(word):
if c == character:
guess[i] = character
if guesses == 0:
print('You LOST!')
break
else:
print('You have only', guesses, 'chances left to win.')
else:
print(''.join(guess))
print('You WON, well done')
我稍微改变了结构,使代码在我看来更直观:
import random
WORDS = ["handkerchief", "accommodate", "indict", "impetuous"]
MAX_GUESSES = 7
print("^^^^^^^^^^THIS IS HANGMAN^^^^^^^^^^")
while True:
input('Press <ENTER> to start a new game or <CTRL>+<C> to quit.')
word = random.choice(WORDS)
guess = ['_'] * len(word)
guesses = set()
n = MAX_GUESSES
while True:
print('\nYour word:', ' '.join(guess))
print('You have {} chances left.'.format(n))
if '_' not in guess:
print('Congratulations, you win!\n')
break
if n < 1:
print('Sorry, no guesses left. You lose!\n')
break
character = input('Guess a new character: ')
if len(character) != 1:
print('You must enter exactly one character!')
continue
if character in guesses:
print('You have already guessed that character!')
continue
guesses.add(character)
if character not in word:
n -= 1
continue
for i, c in enumerate(word):
if c == character:
guess[i] = character
请讲一讲。另外请注意,这是针对特定库的,而不仅仅是因为你要求别人修复你的代码。快速查看:如果lencharacter>1:如果字符不在word中:需要在while loopbreak内使用,这是什么方式?@finefoot:我试图向OP指出,为什么他们的问题不符合网站作为一个用户的指导原则。尝试帮助的人不必在问题中运行代码来了解问题的含义。您知道如何添加一个表示您已经使用过该字母的函数来选择另一个字母吗?您知道如何在每次游戏结束后清除屏幕并重置为菜单吗