gpt4 book ai didi

python - Python 中的刽子手游戏

转载 作者:行者123 更新时间:2023-11-30 22:46:24 25 4
gpt4 key购买 nike

我正在开发一个项目,该项目有一个错误。这出戏再次完美地表达了“不”。但每次我输入“yes”时它都会显示:

TIME TO PLAY HANGMAN
Do you want to play again (yes or no)?

这是我的完整代码

import random

def Hangman():
print ('TIME TO PLAY HANGMAN')

wordlist =['apples', 'oranges', 'grapes', 'pizza', 'cheese', 'burger']
secret = random.choice(wordlist)
guesses = 'aeiou'
turns = 5

while turns > 0:
missed = 0
for letter in secret:
if letter in guesses:
print (letter,end=' ')
else:
print ('_',end=' ')
missed= missed + 1

print

if missed == 0:
print ('\nYou win!')
break

guess = input('\nguess a letter: ')
guesses += guess

if guess not in secret:
turns = turns -1
print ('\nNope.')
print ('\n',turns, 'more turns')
if turns < 5: print ('\n | ')
if turns < 4: print (' O ')
if turns < 3: print (' /|\ ')
if turns < 2: print (' | ')
if turns < 1: print (' / \ ')
if turns == 0:
print ('\n\nThe answer is', secret)

playagain = 'yes'
while playagain == 'yes':
Hangman()
print('Do you want to play again? (yes or no)')
playagain = input()

最佳答案

如果代码在这里看起来像在编辑器中一样,那么您的问题是您没有缩进 print('TIME TO PLAY HANGMAN') 之后的所有代码,因此 python 认为它是这样的位于外部作用域并且仅执行一次。它需要看起来像:

def Hangman():
print ('TIME TO PLAY HANGMAN')
wordlist =['apples', 'oranges', 'grapes', 'pizza', 'cheese', 'burger']
# etc.

playagain = 'yes'
while playagain == 'yes':
# etc.

关于python - Python 中的刽子手游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40891401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com