day_06 猜年龄游戏,三级菜单 ,求1 - 2 + 3 - 4 + 5...99的所有数的和(课后作业)
1.猜年龄游戏:
要求:
允许用户最多尝试3次
每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
如果猜对了,有三次选择奖励的机会,不要奖励可以随时退出,并打印奖品清单给用户看;
count = 0
real_age = 19
goods_list = [
'芭比娃娃',
'变形金刚',
'小猪佩奇',
'蜘蛛侠',
'模型飞机',
'inflatable doll'
]
customer_goods_list = [] while True:
age = input('请猜一下真实年龄(q退出)>>>:').strip().lower()
if age == 'q': break
if not age.isdigit():
print('请输入纯数字!')
continue
age = int(age)
if age == real_age:
print('恭喜你!猜对了!你有3次选择奖励的机会!','\n')
choice_count = 0
while True:
for index, good in enumerate(goods_list, 1):
print(index, good)
choice = input('请选择奖品(或者直接按q退出)>>>:').strip().lower()
if choice == 'q': break
if not choice.isdigit():
print('请输入数字!')
continue
choice = int(choice) - 1
if choice not in range(len(goods_list)):
print('选择的奖品不在范围内,请重新输入!')
continue
else:
choice_count += 1
print(f'你选择的奖品是:{goods_list[choice]},还有{3-choice_count}次机会!')
customer_goods_list.append(goods_list[choice]) if choice_count == 3: break
print(f'你的奖品清单:{customer_goods_list}')
break elif age > real_age:
print('猜大了!')
continue
else:
print('猜小了!')
count += 1
if count == 3:
while True:
again_choice = input('还要再玩三次吗?输y再玩3次,或者输n直接退出>>>:').strip().lower()
if again_choice == 'y':
count = 0
continue
elif again_choice == 'n':
exit()
#quit()
else:
print('输入不正确,请重试!')
2.三级菜单:
打印省、市、县三级菜单
可返回上一级
可随时退出程序
menu = {
'北京': {
'海淀': {
'五道口': {
'soho': {},
'网易': {},
'google': {}
},
'中关村': {
'爱奇艺': {},
'汽车之家': {},
'youku': {},
},
'上地': {
'百度': {},
},
},
'昌平': {
'沙河': {
'老男孩': {},
'北航': {},
},
'天通苑': {},
'回龙观': {},
},
'朝阳': {},
'东城': {},
},
'上海': {
'闵行': {
"人民广场": {
'炸鸡店': {}
}
},
'闸北': {
'火车战': {
'携程': {}
}
},
'浦东': {},
},
'山东': {},
}
layers = [menu, ]
while True:
if len(layers) == 0: break
current_layers = layers[-1]
# print(current_layers,type(current_layers),'=================')
for key in current_layers:
print(key)
choice = input('请选择(b返回上一层,q直接退出)>>>:').strip()
if choice == 'b':
layers.pop(-1)
# print(layers,'+++++++++++++++++')
continue
if choice == 'q': break
if choice not in current_layers:
continue
else:
layers.append(current_layers[choice])
# for i in layers:
# print(i,'@@@@@@@@@@@@@@@@@@@@')
# print(layers,'-------------------------',len(layers))
# print(current_layers[choice],type(current_layers[choice]),'**********************')
3.求1 - 2 + 3 - 4 + 5...99的所有数的和
# 求1 - 2 + 3 - 4 + 5...99的所有数的和 # 方法1
count = 0
get_sum = 0
while True:
if count == 100:
break
get_sum += count * ((-1) ** (count + 1))
count += 1
print(get_sum) # 方法2
count = 0
get_sum = 0
get_sum1 = 0
get_sum2 = 0
while True:
if count == 100:
break
if count % 2 == 0:
get_sum1 -= count
else:
get_sum2 += count
get_sum = get_sum1 + get_sum2
count += 1
print(get_sum)
day_06 猜年龄游戏,三级菜单 ,求1 - 2 + 3 - 4 + 5...99的所有数的和(课后作业)的更多相关文章
- python猜年龄游戏升级版
猜年龄游戏升级版 要求:允许用户最多尝试3次,每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y,就继续让其猜3次,以此往复,如果回答N,就退出程序,如何猜对了,就直接退出 age = 1 ...
- python基础实战之猜年龄游戏
目录 一.Python基础实战之猜年龄游戏 给定年龄,用户可以猜三次年龄 年龄猜对,让用户选择两次奖励 用户选择两次奖励后可以退出 age = 18 # 答案 count = 0 # 游戏次数控制 p ...
- day_10猜年龄游戏函数版
''' 1. 在猜年龄的基础上编写登录.注册方法,并且把猜年龄游戏分函数处理,如 2. 登录函数 3. 注册函数 4. 猜年龄函数 5. 选择奖品函数 ''' import json real_age ...
- day07作业猜年龄游戏
# 给定年龄,用户可以猜三次年龄 # # 年龄猜对,让用户选择两次奖励 # # 用户选择两次奖励后退出 get_prize_dict = {} # 获取的奖品信息 age = 18 inp_count ...
- Python练习-猜年龄的LowB游戏
Alex大神今天让我做一个猜年龄的游戏: 第一个游戏是你只能猜三次:真的很LowB啊~ # 编辑者:闫龙 #猜年龄游戏,3次后程序自动退出! ages = 29; #for循环3次 for i in ...
- 用python写了一个猜年龄小游戏
写一个猜年龄游戏: 需要实现用户登录的功能 初始用户登录信息为 {'hades': '13579','nick': '123','ruixing': 'a1','fanping': 'b2'} 登录时 ...
- Python3 猜年龄小游戏进阶之函数处理
在猜年龄的基础上编写登录.注册方法,并且把猜年龄游戏分函数处理 登录函数 注册函数 猜年龄函数 选择奖品函数 # 注册 def register(): '''注册''' count = 0 while ...
- day03_11 if语句实现猜年龄01
老男孩猜年龄游戏 age_of_princal = 56 guess_age = int( input(">>:") ) #以下为伪代码 ''' if guess_ag ...
- Python字符串内置方法使用及年龄游戏深入探究
目录 作业 ==程序代码自上往下运行,建议自上而下的完成下列任务== 作业 使用代码实现以下业务逻辑: 写代码,有如下变量name = " aleX",请按照要求实现每个功能: 移 ...
随机推荐
- 配置XFCE4的时钟显示格式
配置XFCE4的时钟显示格式,如下1: %b%d %A, %R:%S 显示结果: 10月09日 星期三,09:50:45 如下2: [%Y年%b %d日] [%A],第%V周,第%j天 显示结果: 2 ...
- docker下载镜像加速
1.如何获取阿里云加速地址 参考文档 https://yq.aliyun.com/articles/29941 关于加速器的地址,你只需要登录容器Hub服务的控制台,左侧的加速器帮助页面就会显示为你独 ...
- The window object
At the core of the BOM is the window object, which represents an instance of the browser. The window ...
- dapper使用时性能优化
数据库中类型 Area 数据库类型 varchar dapper 来操作数据库,不能直接写 sql Area=@Area) //dapper 对C#中的字符串类型 默认是对应数据库nva ...
- ETH 全节点的远程Debug环境搭建
ETH全节点还是很浪费资源的,尤其是在同步下来所有区块链,如果你打算在本地进行全节点的Debug吗,有点不现实.这个文档 编译,在要运行的机器上面安装devel,一般的方法是,编译好之后,放到服务器上 ...
- 【VS开发】使用WinPcap编程(4)——把网络数据包存储到一个文件中
这里用到的数据结构是pcap_dumper_t,这也是一个相当于文件描述符的东西,我们在用的时候先指定pcap_dumper_t *dumpfp; 使用两个函数来存储网络数据,一个是pcap_dump ...
- ocelot集成consul服务发现
首先下载consul 点击这里下载 转到解压文件夹目录输入cmd命令 consul agent -dev (有时候会卡住按一下方向键上) 在浏览器中输入http://localhost:8500/u ...
- shiro登陆认证
1.LoginController @RequestMapping(method = RequestMethod.POST) public String login(User user, HttpSe ...
- redhat java配置
原来的java版本为1.4 whereis java后 将java原来的目录全部删除 拿来新的1.8的安装好的java包 vi /etc/profile插入 JAVA_HOME=/usr/local/ ...
- layer弹出框的简易封装和使用
1. 封装layer 下载layer绿色版和jquery引入页面 <!DOCTYPE html> <html lang="zh-CN"> . . . < ...