Python之双色球选购和三级菜单问题
# 1 双色球(假设一共八个球,6个红球,球号1-32、2个蓝球,球号1-16)
# 2 确保用户不能重复选择,不能超出范围
# 3 用户输入有误时有相应的错误提示
# 4 最后展示用户选择的双色球的号码
while True:
n = int(input('请输入你要选择的红色球(1-32):'))
if 0 < n < 33:
if n not in select_red_ball:
select_red_ball.append(n)
else:
print('number %d is already exist in red ball list' % n)
else:
print('only can select n between 1-32')
if len(select_red_ball) == 6:
break
select_red_ball.sort()
select_blue_ball = []
while True:
n = int(input('请输入你要选择的蓝色球(1-32):'))
if 0 < n < 17:
if n not in select_blue_ball:
select_blue_ball.append(n)
else:
print('number %d is already exist in blue ball list' % n)
else:
print('only can select n between 1-16')
if len(select_blue_ball) == 2:
break
select_blue_ball.sort()
print('red ball %d' % select_red_ball)
print('blue ball %d' % select_blue_ball)
2 :三级菜单
数据结构:
menu = {
'北京':{
'海淀':{
'五道口':{
'soho':{},
'网易':{},
'google':{}
},
'中关村':{
'爱奇艺':{},
'汽车之家':{},
'youku':{},
},
'上地':{
'百度':{},
},
},
'昌平':{
'沙河':{
'老男孩':{},
'北航':{},
},
'天通苑':{},
'回龙观':{},
},
'朝阳':{},
'东城':{},
},
'上海':{
'闵行':{
"人民广场":{
'炸鸡店':{}
}
},
'闸北':{
'火车站':{
'携程':{}
}
},
'浦东':{},
},
'山东':{},
} 需求:
可依次选择进入各子菜单
可从任意一层往回退到上一层
可从任意一层退出程序
所需新知识点:列表、字典
代码一:
exit_flag = False
while not exit_flag:
for i in menu:
print(i)
choice1 = input("选择进入1>>:")
if choice1 in menu:
while not exit_flag:
for i2 in menu[choice1]:
print("\t", i2)
choice2 = input("选择进入2>>:")
if choice2 in menu[choice1]:
while not exit_flag:
for i3 in menu[choice1][choice2]:
print("\t\t", i3)
choice3 = input("选择进入3>>:")
if choice3 in menu[choice1][choice2]:
for i4 in menu[choice1][choice2][choice3]:
print("\t\t", i4)
choice4 = input("最后一层,按b返回>>:")
if choice4 == "b":
pass
elif choice4 == "q":
exit_flag = True
if choice3 == "b":
break
elif choice3 == "q":
exit_flag = True
if choice2 == "b":
break
elif choice2 == "q":
exit_flag = True
代码二:(优化版本,代码缩减为15行)
current_layer = menu
parent_layer = [] # 将父级key值放入到列表中
flags = False # 设置标志位
while not flags:
for key in current_layer:
print(key)
choose = input("请选择,输入b返回上一级菜单,输入q退出菜单:").strip() # 去除空格
if choose in current_layer:
parent_layer.append(current_layer) # 将当前的状态放入列表中
current_layer = current_layer[choose]
elif choose == 'b':
if parent_layer:
current_layer = parent_layer.pop()
elif choose == 'q':
flags = True
else:
print('输入有误,请重新输入')
Python之双色球选购和三级菜单问题的更多相关文章
- 老男孩python学习之作业二---三级菜单
因为之前花力气完成了购物小程序的作业 现在做这个三级菜单简直是so easy!! 1.显示省级菜单 2.交互,提示用户输入要查看的省份(退出e) 2.1.用户正确输入列表中的省份 3.显示市级菜单 3 ...
- day17 python递归案例(二分查找,三级菜单)
递归函数与三级菜单 menu = { '北京': { '海淀': { '五道口': { 'soho': {}, '网易': {}, 'google': {} }, '中关村': { '爱奇艺': {} ...
- (Python基础)最Low三级菜单
#-*-coding:utf-8-*- #_author_: Keep #三级菜单 menu = { '中国':{ '广东省':{ '广州市':{ '海珠区':{}, '荔湾区':{}, '越秀区': ...
- Python初学者第十三天 三级菜单程序小作业
13day 作业题目: 三级菜单 作业需求: 数据结构: menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{}, '网易':{}, 'google':{} }, '中关村' ...
- Python学习第二天-编写三级菜单
编写三级菜单:1. 运行程序输出第一级菜单2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单3. 菜单数据保存在文件中4. 让用户选择是否要退出5. 有返回上一级菜单的功能 # Author: z ...
- python小程序--Three(三级菜单)
#!/usr/bin/env python # _*_ coding:utf8 _*_ data = { "山东省":{ "滨州市":{"惠民县&qu ...
- Python小代码_4_省市区三级菜单
menu = { "北京": { "朝阳区": { "三环到四环之间": {}, "四环到五环之间": {}, &quo ...
- python练习1 登录和三级菜单
,: username1 = input("请输入您的用户名:")# password1 = getpass.getpass("请输入您的密码:") passw ...
- python学习笔记(字符串操作、字典操作、三级菜单实例)
字符串操作 name = "alex" print(name.capitalize()) #首字母大写 name = "my name is alex" pri ...
随机推荐
- codevs——1080 线段树练习
1080 线段树练习 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 一行N个方格,开始每个格子里都有 ...
- webpack 输出多个文件
http://react-china.org/t/webpack/1870/2 webpack 文章 entry = { "button": "demo/button/i ...
- 华为OJ1964-求解立方根(牛顿迭代法)
一.题目描述 描述: 计算一个数字的立方根,不使用库函数. 函数原型double getCubeRoot(double input) 输入: 待求解参数 double类型 输出: 输出参数的立方根,保 ...
- Hadoop之——HBASE结合MapReduce批量导入数据
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46463889 废话不多说.直接上代码,你懂得 package hbase; imp ...
- 2016/1/17 private public protected 权限练习
package caipiao; private class Test0117 { private String title; public String actor; protected Strin ...
- 使用 dva + antd 快速开发react应用
使用 dva + antd 快速开发react应用 版本说明: 注意:dva的版本是0.9.2 $ node -v v10.2.1 $ npm -v $ dva -v dva-cli version ...
- HDU 5783Divide the Sequence
Divide the Sequence Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- POJ 2636:Electrical Outlets
Electrical Outlets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9597 Accepted: 718 ...
- UIColor与十六进制字符串互转
UIColor转十六进制字符串 方法一:经过测试发现部分颜色会转化为#fffff(五个f),转化残缺导致最后颜色无法还原,不推荐使用. // UIColor转#ffffff格式的字符串 + (NSSt ...
- javaSE基础(一)
说明: 1)本系列专综合java SE 之基础概念!因为个人觉得,许多知识点的不理解来自于对各种名称与概念的定义的不理解. 2)其中的定义参考来自于Stuart Reges 和 Marty Stepp ...