python基础:多级菜单

#类似于多级菜单中的经典列子---城市的多级菜单
#个人更改了组成成分,效果大致不变,总体还是臃肿的,还在练习中,算是基本能实现效果了
#---基本功能---
#1.用户能根据选择进入菜单
#2.用户能返回上一级以及能够退出
#3.具体商品的相关介绍也能打印出来


city = {
'书籍':{
'python书籍':{
'python基础':{'python的基础书籍'},
'python进阶':{'python进阶用的'},
'python进阶到放弃':{'python开始放弃'}
},
'美食书籍':{
'炒菜指南':{'教你炒菜咯'},
'美食菜谱':{'看看有什么美食'},
'菜品介绍':{'关于菜品的介绍以及由来'}
}
},
'饮料':{
'能量饮料':{
'红牛':{'喝了一晚上不睡觉'},
'战马':{'喝了奋战到天明'},
'魔爪':{'喝了提神哟'}
},
'普通饮料':{
'矿泉水':{'就普通的纯净水'},
'冰红茶':{'这是一瓶红茶'},
'乌龙茶':{'这是一瓶乌龙茶'}
}
},
'汽车':{
'丰田':{
'CROWN皇冠':{'皇冠车'},
'COROLLA卡罗拉':{'卡罗拉哟'},
'COROLLA花冠':{'花冠经典车'}
},
'劳斯莱斯':{
'银色精灵':{'别看了,买不起'},
'ParkWard':{'豪华座驾'},
'100EX':{'CEO专用'}
}
}
}



def Main():
first_stage()



#第一级菜单
def first_stage():
print('请根据下列的序号查找!')
first_list = [] #['书籍', '饮料', '汽车']
count = 1
for key in city.keys():
first_list.append(key)
for val in first_list:
print(str(count) + '-' + val)
count = count + 1
number = input('请输入你要选择的序号(任意键退出):')
if number.isdigit() == False:
print('您已退出!')
exit()
elif int(number) > len(first_list):
print('您输入的选项是不存在的!')
first_stage()
elif int(number) == 0:
print('您输入的选项是不存在的')
first_stage()
else:
number = int(number) - 1
second_stage(number,first_list)





#二级菜单
def second_stage(number,list_name):
dict_second = city[list_name[number]] #二级菜单下的key
tat = [number,list_name]
second_list = []
count = 1
for key in dict_second.keys():
print(str(count)+'-'+key)
count = count + 1
second_list.append(key)
is_num = input('请输入你要选择的序号(任意键返回上一级):')
if is_num.isdigit() == False:
print('您已退出当前界面!1')
first_stage()
elif int(is_num) > len(second_list):
print('您输入的选项是不存在的!')
second_stage(number,list_name)
elif int(is_num) == 0:
print('您输入的选项是不存在的')
second_stage(number,list_name)
else:
is_num = int(is_num) - 1
three_stage(is_num,second_list,dict_second,tat)




#三级菜单
def three_stage(is_num,list_name,dict_second,tat):
dict_three = dict_second[list_name[is_num]]
_three = [is_num,list_name,dict_second,tat]
three_list = []
count = 1
for key in dict_three:
print(str(count)+'-' +key)
three_list.append(key)
count = count +1
is_three = input('请输入你要选择的序号(任意键返回上一级):')
if is_three.isdigit() == False:
print('您已退出当前界面!')
second_stage(tat[0],tat[1])
elif int(is_three) > len(three_list):
print('您输入的选项是不存在的!')
three_stage(is_num,list_name,dict_second,tat)
elif int(is_three) == 0:
print('您输入的选项是不存在的')
three_stage(is_num,list_name,dict_second,tat)
else:
is_three = int(is_three) - 1
multistage(is_three,three_list,dict_three,_three)


#以下的多级,判断用户还可以在做优化,有时间了在改改
def multistage(is_mu,list_mu,dict_list,tat):
dict_mu = dict_list[list_mu[is_mu]]
mu_one = []
for key in dict_list.keys():
mu_one.append(key)
print(str(mu_one[is_mu]) + ':' + str(dict_mu))
is_Nan = input('输入任意键返回上一级(退出程序:q):')
if is_Nan == 'q':
exit()
three_stage(tat[0],tat[1],tat[2],tat[3])


Main()

转载于:https://www.cnblogs.com/ifdashui/p/10617689.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值