基本挂人游戏Python

时间:2015-03-08 07:03:14

标签: python

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

我如何能够添加用户可以获胜的方式?我已经尝试将猜测的字母与原始单词进行比较但是没有用。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

最简单的实施。添加一个初始化为won的布尔变量True;如果打印了_,请将其设置为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!")