python简单小游戏代码-python基础练习之几个简单的游戏

文档介绍

利用python写"猜数字”,"猜词语”,"谁是卧底”这三个游戏,从而快速掌握python编程的入门知识,包括python语法/列表/元组/字典/流程控制/库函数等等。

环境参数

linux平台,python3.4。需要在linux中把python3.4编译一下,这样编写python程序时保存为.py格式的文件并添加执行权限再终端运行即可(原理跟shell脚本相同),非常方便。

ps:贴出来的python程序代码在windows中也兼容,只不过运行方式不是命令行,而是要通过python软件。python软件可以上官网下载安装点击打开链接,在学习的时候,可以直接在python软件上面输入python语句即可打印结果,也就是说python无需编译即可运行,很多人也称之为"脚本”语言。

设计实现

猜数字

很多人在聚餐时都玩过猜数字游戏,由某人随机出一个指定范围内的数,然后其他人一个一个猜,猜的过程中区间不断缩小,直到猜中为止。

这里的猜数字游戏就是用程序来代替那个出数字的人,程序算法设计为:

1.输入数字区间--->2.系统产生区间内的随机数--->3.玩家输入自己猜的数字--->4.比较玩家猜的与答案的高低并提示--->5.未猜中则回到3,猜中则提示猜次数

代码很少,如下所示:

#!/usr/local/bin/python3

import random

bot=int(input('Set range bottom '))

top=int(input('Set range top '))

rand=random.randint(bot,top)

print ('Random number in ['+str(bot)+','+str(top)+'] generated!')

num=int(input('###Guess the number### '))

cnt=1

while (num!=rand):

if (num

print('*_* Lower than the answer')

else:

print('T_T Higher than the answer')

num=int(input('###Guess the

  • 4
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值