word = input('Enter a word to guess: ')
for n in range(50):
print('')
Lives = 10
playing = True
lettersGuessed = ''
while playing == True:
print(str(Lives)+' Lives left')
if Lives == 0:
playing = False
print('You lose')
else:
for char in word:
if char in lettersGuessed:
print(char, end=' ')
else:
print('_', end=' ')
guess = input('\nEnter a letter to guess: ')
if guess in word:
lettersGuessed += guess
else:
Lives = Lives-1我如何才能添加一种让用户获胜的方法?我试着将猜测的字母与原始单词进行比较,但不起作用。任何帮助都是非常感谢的。
发布于 2015-03-08 15:23:28
最简单的实现。添加一个初始化为True的布尔变量won;如果打印了任何_,则将其设置为False。只有在没有打印_的情况下,它才保持为True:
won = True
for char in word:
if char in lettersGuessed:
print(char, end=' ')
else:
print('_', end=' ')
won = False
if won:
print("Hey, you win!")https://stackoverflow.com/questions/28923964
复制相似问题