python 猜词游戏

猜字游戏

在这个游戏中,你必须一个字母一个字母的猜出秘密单词。

如果你猜错了一个字母,你将丢掉一条命。

正如游戏名那样,你需要仔细选择字母,因为你的生命数量非常有限。

import  random
lives = 3

 # 系统随机抽取单词
word  = ['pizza', 'fairy', 'teeth', 'shirt', 'otter', 'plane' ]
secret_word = random.choice(word)
print(secret_word)

heart_symbol = u'\u2764'
print('剩余生命次数:', heart_symbol * lives)

guess_word_correctly = False
# 创建了一个列表
clue = list('?????')

def update_clue(guess_letter,secret_word,clue):
    # index 通常是一个用于标识列表中某个元素位置的整数
    index = 0
    while index < len(secret_word):
        if guess_letter == secret_word[index]:
            clue[index] = guess_letter
        index = index + 1
while lives > 0:
    print(clue)

    guess = input(" 请输入你想要猜的单词:").lower()
    print(guess)

    if guess == secret_word:
        guess_word_correctly = True
        break
    elif guess in secret_word:
        update_clue(guess,secret_word,clue)
    else:
        print('错误。你丢了一条命\n')
        lives = lives - 1
        print('剩余生命次数:' ,heart_symbol* lives)


if guess_word_correctly :
    print('你赢了! 秘密单词是 ' + secret_word)
else:
    print('你输了! 秘密单词是 ' +  secret_word)

 

 

pizza
剩余生命次数: ❤❤❤
['?', '?', '?', '?', '?']
 请输入你想要猜的单词:d
d
错误。你丢了一条命

剩余生命次数: ❤❤
['?', '?', '?', '?', '?']
 请输入你想要猜的单词:p
p
['p', '?', '?', '?', '?']
 请输入你想要猜的单词:z
z
['p', '?', 'z', 'z', '?']
 请输入你想要猜的单词:i
i
['p', 'i', 'z', 'z', '?']
 请输入你想要猜的单词:q
q
错误。你丢了一条命

剩余生命次数: ❤
['p', 'i', 'z', 'z', '?']
 请输入你想要猜的单词:a
a
['p', 'i', 'z', 'z', 'a']
 请输入你想要猜的单词:pizza
pizza
你赢了! 秘密单词是 pizza

Process finished with exit code 0

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是几个简单的Python游戏的示例代码: 1. 数字游戏: ```python import random number = random.randint(1, 100) guess = int(input("一个1到100之间的数字:")) while guess != number: if guess < number: print("小了!") else: print("大了!") guess = int(input("再一次:")) print("恭喜你,对了!") ``` \[1\] 2. 石头剪刀布游戏: ```python import random choices = \["石头", "剪刀", "布"\] computer_choice = random.choice(choices) user_choice = input("请输入你的选择(石头、剪刀、布):") print("电脑选择:", computer_choice) print("你的选择:", user_choice) if user_choice == computer_choice: print("平局!") elif (user_choice == "石头" and computer_choice == "剪刀") or (user_choice == "剪刀" and computer_choice == "布") or (user_choice == "布" and computer_choice == "石头"): print("你赢了!") else: print("你输了!") ``` \[1\] 3. 游戏: ```python words = \["apple", "banana", "orange", "grape", "watermelon"\] word = random.choice(words) guess = input("一个水果的名字:") while guess != word: print("错了!") guess = input("再一次:") print("恭喜你,对了!") ``` \[1\] 这些游戏只是Python编程中的一小部分应用,希望能给您带来一些乐趣和启发。如果您对其他Python编程方面有兴趣,可以继续学习Python的列表、元组、字典、字符串等相关知识,或者尝试使用Python进行爬虫、人脸识别等实际应用。\[2\]在学习Python时,您可以下载并安装Python软件,通过该软件运行Python代码,无需编译即可运行,这也是Python被称为"脚本"语言的原因之一。\[3\]希望这些信息对您有所帮助,如果有任何问题,请随时留言,我会尽快回复。 #### 引用[.reference_title] - *1* *2* *3* [python简单小游戏代码-python基础练习之几个简单的游戏](https://blog.csdn.net/weixin_37988176/article/details/109368681)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值