3个版本的三级菜单

menu= {
'河北省':{
'石家庄市':{
'井陉','正定'},
'唐山市':{
'乐亭','迁西'},
'唐山1市':{
'乐亭','迁西'},
'唐山2市':{
'乐亭','迁西'}},
'辽宁省':{
'沈阳市':{
'康平','法库'},
'大连市':{
'中山','西岗'}},
'吉林省':{
'长春市':{
'农安','南关'},
'吉林市':{
'永吉','蛟河'},}
}
back_flag=False
Back_flag=False
while not Back_flag :
for key in menu:
print(key)
chioce= input('>>:').strip()
if chioce=='B':
Back_flag=True
if chioce in menu:
while not back_flag:
for key2 in menu[chioce]:
print(key2)
chioce2 =input('>>:')
if chioce2 in menu[chioce]:
print(menu[chioce][chioce2])
print('last page')
else:
back_flag=True

另一个版本
menu= {
'河北省':{
'石家庄市':{
'井陉':{},'正定':{}},
'唐山市':{
'乐亭':{},'迁西':{}},},
'辽宁省':{
'沈阳市':{
'康平':{},'法库':{}},
'大连市':{
'中山':{},'西岗':{}}},
'吉林省':{
'长春市':{
'农安':{},'南关':{}},
'吉林市':{
'永吉':{},'蛟河':{}},}
}

current_layer=menu #先给字典赋值
parent_list=[] #建立一个空列表
while True:
for key in current_layer: #输出键
print(key)
choice=(input('>>>>:')).strip() #输入查看的目标
if len(choice) == 0:continue # 判断输入内容的长度
if choice in current_layer:
current_layer=current_layer[choice] # 将'输入查看的目标'对应的级别赋值给current_layer
parent_list.append(current_layer) # 在进入下一级别之前将当前的级别的内容添加到列表之中 方便小退出时使用
elif choice=='b':
if parent_list:
current_layer=parent_list.pop() #选择退出后 之前保留的上一级别的内容就会赋值给current_layer 就可以返回上一级
else :
print('it is topside')
else:
print('no this option'
这里我添加一个生华版:会用到文件的部分知识
menu= {
'河北省':{
'石家庄市':{
'井陉':{},'正定':{}},
'唐山市':{
'乐亭':{},'迁西':{}},},
'辽宁省':{
'沈阳市':{
'康平':{},'法库':{}},
'大连市':{
'中山':{},'西岗':{}}},
'吉林省':{
'长春市':{
'农安':{},'南关':{}},
'吉林市':{
'永吉':{},'蛟河':{}},}
}
a =str(menu)
with open('caidan','w',encoding='utf8') as f:
f.write(a)
with open('caidan', 'r', encoding='utf8') as f:
menu = eval(f.read().strip())
current_layer= menu
parent_list=[]
while True:
for key in current_layer:
print(key)
chioce=input('chioce which[add]/[del]/[revise]:').strip()
if len(chioce)==0:
continue
if chioce in current_layer:
parent_list.append(current_layer)
current_layer=current_layer[chioce]
elif chioce =='add':
new_one=input('what you want add:').strip()
if new_one in current_layer:
print('you choice has existed')
else :
current_layer[new_one]={}
continue
elif chioce =='del':
del_one=input('which one you want del:').strip()
if del_one in current_layer:
parent_list.append(current_layer)
del current_layer[del_one]
continue
else:
print('the project not exist')
elif chioce=='revise':
revise_one=input('fill which one you want revise:').strip()
if revise_one in current_layer:
revise_one_after=input('fill in revise after name:').strip()
current_layer[revise_one_after]=current_layer[revise_one]
del current_layer[revise_one]
continue
else :
print('plese fill right format')
elif chioce == 'b':
if parent_list:
current_layer=parent_list.pop()
else:
print('it is topside')
break
else:
print('fill ture')
if type(current_layer) != list:
print(current_layer)
with open('menu','w',encoding='utf8') as f:
f.write(str(current_layer))



posted @ 2019-08-21 12:15  ComeIntoBud  阅读(184)  评论(0)    收藏  举报