我正在处理某个项目,但它有一个错误。该剧再次适合'不'。但每次输入“是”时它都会显示:
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()
答案 0 :(得分:2)
如果代码看起来像在编辑器中那样,那么问题是你没有在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.
答案 1 :(得分:1)
你Hangman
函数唯一能做的就是打印“玩游戏的时间”。其他一切都在功能之外。修复你的缩进,将游戏玩法循环放在函数中,它应该可以工作。
答案 2 :(得分:0)
你陷入了while循环:
playagain = 'yes'
while playagain == 'yes':
Hangman()
print('Do you want to play again? (yes or no)')
playagain = input()
你的循环一直在寻找是否playagain == 'yes'
。由于输入yes,因此运行while循环的条件仍然为真,这就是它再次运行并打印语句的原因。
我没有运行您的代码或检查其余部分,但根据您提供的问题,这应该是您的修复。