初学Python实现猜字游戏(Hangman)

猜字游戏
要求:
创建一个经典的美国游戏,这是一个猜谜游戏。用户需要通过输入逐个字符来猜测一个单词。

要猜测的词由一排- - - - - - -表示(每一个‘-’代表相应字母的位置)。
如果玩家猜到单词中存在的字母,脚本会以所有正确的位置写入该字母。
玩家有10个回合来猜单词。

您可以通过更改变量轻松自定义游戏。您将使用计数器和循环。示例执行:
Secret Word: - - - - - - -
Enter Letter: a
Output: There is no letter ‘a’ in the secret word. You have 9 tries left
Enter Letter: d
Secret Word: - - d - - - -
等等。

如果用户在最大猜测次数之前猜到了正确的单词,则将显示单词和消息“Congratulations: You guessed the correct WORD”。比如Secret Word: Midterm. Congratulations: You guessed the correct WORD.
如果用户在最大猜测次数之前没有猜到正确的单词,则将显示一条消息。 例如:You have reached your maximum guesses. The secret word was Midterm. You failed to guess the secret word.

请记住,用户最多只能猜测 10 次。

注意:该字母将更换为其所有位置。 例如:如果机密单词是Welcome,并且用户猜到了字母"e"。 字母"e"在单词中出现 2 次,因此在字母的所有匹配项中替换短划线。 例如: - e - - - - e

Secret_word = 'welcome'
SW = list(Secret_word)
Guess_word = ['-'] * len(Secret_word)
print('Secret Word:',end=' ')
for i in range(len(Guess_word)):
    print(Guess_word[i],end=' ')
print()
count=0
while True:
    Guess_letter = input('Enter Letter:')
    if Guess_letter not in Secret_word:
        count = count + 1
        print('Output: There in no letter %s in the secret word. You have  %d tries left'%(Guess_letter,10-count))
    else:
        print("Secret Word: ")
        for j in range (len(Secret_word)):
            if Guess_letter == Secret_word[j]:
                Guess_word[j] = Guess_letter
        for i in range(len(Guess_word)):
            print(Guess_word[i],end=' ')
        print()
    if Guess_word == SW:
        print('Sercet Word: '+Secret_word+'. Congratulations: You guessed the correct WORD.') 
        break  
    if 10-i == 0:
        print('You have reached your maximum guesses. The secret word was '+Secret_word+'. You failed to guess the secret word.') 
        break      
                  

觉得有用请点个赞吧,谢谢.

  • 8
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值