Python攻克之路-三级菜单(重复代码的实现)
三级菜单实现要求
1.能够一层一层的进入到所有层
2.可以在每层返回上一层
3.可以在任意层退出主菜单
实现要点:要进入菜单要使用循环进入每个菜单
操作实现:
a.让它保持循环状态,进入到菜单选择,给用户输入选择,strip去掉一些换行符
while True:
for key in menu:
print(key)
choice = input("1>>: ").strip()
b.判断用户所输入的choice是否在菜单里,如果在就进入到第二层循环,menu[choice]表示如beijing是menu[beijing]
在beijing里面的循环,当用户进入到beijing,如果任意输入不存在的地名,就会返回到上一层的循环,因为循环到这里已经结束了,所以要让它保留在第二层的循环里要使用while True
if choice in menu:
while True:
for key2 in menu[choice]:
print(key2)
choice2 = input("2>>: ").strip()
c.写进入第三层循环,如果输入的是changping,menu下面只有beijing,shanghai,shandong,如时if changping在这三个下面是找不到的,它只知道changping在beijing下面,所以在if choice2 in menu[beijing],这种形式,它才可以找到,while True: #要使用while,否则到第三层后,再输入还是会返回到上一级
while True: #要使用while,否则到第三层后,再输入还是会返回到上一级
if choice2 in menu[choice]: #如果changping在beijing下
for key3 in menu[choice][choice2]: #进入到changping下面
print(key3)
choice3 = input("3>>: ").strip()
d.实现进入第四层循环
if choice3 in menu[choice][choice2]:
while True:
for key4 in menu[choice][choice2][choice3]:
print(key4)
choice3 = input("4>>: ").strip()
e.解决回退的issue:标志位
back_flag = False #总的上加上全局标志,默认为false
while not back_flag: #所有的while都加上,如果不为真都一直执行
if choice4 == "b": #
back_flag = True
分析:最后输入b会退出程序,因为while not back_flag设置为True,条件不成立就不往下走了,这个循环就退出了,相当于break,会往下走,但是下面没有代码了,会往上一层走,同样back_flag也是True,也会再往上一层走,直到最顶层也是一样,所以没有实现返回上一层的功能,却实现退出主目录
while not back_flag:
for key4 in menu[choice][choice2][choice3]:
print(key4)
choice4 = input("4>>: ").strip
print('last level')
if choice4 == "b":
back_flag = True
f.返回上一层的实现,在每个循环后加上if判断,在最后加个else back_flag为False就返回
if choice3 == "b": #每层都加上
back_flag = True
while not back_flag: 为真向下
else:
back_flag = False #否则为假返回
g.程序在任意层实现退出,也通过标志为来实现
exit_flag = False #全局加
while not back_flag and not exit_flag: #每层循环加上exit_flag,只有没返回和退出程序才会向下走
if choice == "q": #每层后加上相应的退出标志
exit_flag = True
菜单代码:
1 menu = { 2 'Beijing':{ 3 'Chaoyang':{ 4 'Guomao':{ 5 'CICC':{}, 6 'HP':{}, 7 'Zadayinhang':{}, 8 'CCTV':{}, 9 }, 10 'Wangjing':{ 11 'Momo':{}, 12 'Benz':{}, 13 '360':{}, 14 }, 15 'Sanlitun':{ 16 'Youyiku':{}, 17 'apple':{}, 18 }, 19 }, 20 'Changping':{ 21 'Shahe':{ 22 'Etiantian':{}, 23 'Atai':{}, 24 }, 25 'Tiantongyan':{ 26 'Lianjia':{}, 27 'Woaiwojia':{}, 28 }, 29 'Huilongguan':{}, 30 }, 31 'Haidian':{ 32 'Wudaokou':{ 33 'Google':{}, 34 'Wangyi':{}, 35 'Shouhu':{}, 36 'Sogu':{}, 37 'Kuaishou':{}, 38 }, 39 "Zhongguancun":{ 40 'Youku':{}, 41 'Iqiyi':{}, 42 'Qichezhijia':{}, 43 'Xindongfang':{}, 44 'qq':{}, 45 } 46 }, 47 }, 48 'Shanghai':{ 49 'Budong':{ 50 'Lujia':{ 51 'CICC':{}, 52 'Gaosheng':{}, 53 'Mogeng':{}, 54 }, 55 56 'Waitang':{}, 57 }, 58 'Minhang':{}, 59 'Jingan':{}, 60 }, 61 'Shandong':{ 62 'Jinan':{}, 63 'Dezhou':{ 64 'Leling':{ 65 'Dingwuzhen':{}, 66 'Chenqu':{}, 67 }, 68 'Pingyuan':{}, 69 }, 70 'Qindao':{}, 71 }, 72 }
代码正文:
#!/usr/local/python3/bin/python3
back_flag = False
exit_flag = False
while not back_flag and not exit_flag:
for key in menu:
print(key)
choice = input("1>>: ").strip()
if choice == "q":
exit_flag = True
if choice in menu:
while not back_flag and not exit_flag:
for key2 in menu[choice]:
print(key2)
choice2 = input("2>>: ").strip()
if choice2 == "b":
back_flag = True
if choice2 == "q":
exit_flag = True
if choice2 in menu[choice]:
while not back_flag and not exit_flag:
for key3 in menu[choice][choice2]:
print(key3)
choice3 = input("3>>: ").strip()
if choice3 == "b":
back_flag = True
if choice3 == "q":
exit_flag = True
if choice3 in menu[choice][choice2]:
while not back_flag and not exit_flag:
for key4 in menu[choice][choice2][choice3]:
print(key4)
choice4 = input("4>>: ").strip()
print('last level')
if choice4 == "b":
back_flag = True
if choice4 == "q":
exit_flag = True
else:
back_flag = False
else:
back_flag = False
else:
back_flat = False
测试:
[root@zabbix day7]# python3 menu.py
Beijing
Shanghai
Shandong
1>>: Beijing
Chaoyang
Changping
Haidian
2>>: Chaoyang
Guomao
Wangjing
Sanlitun
3>>: Wangjing
Momo
Benz
360
4>>: Benz
last level
Momo
Benz
360
4>>: b
last level
Guomao
Wangjing
Sanlitun
3>>: b
Chaoyang
Changping
Haidian
2>>: q

浙公网安备 33010602011771号