python三级菜单_python三级菜单

这篇博客介绍了如何使用Python实现一个三级菜单的交互功能。通过字典数据结构存储菜单层级,并利用循环和条件判断来实现菜单的逐级展示和返回操作。示例中展示了两种不同的实现方法,帮助读者理解如何在Python中创建和操作复杂菜单结构。
摘要由CSDN通过智能技术生成

#作者:刘亮辉

#时间:2019/3/7

#打印三级菜单

#定义一个字典:

'''定义一个三级菜单'''

mnue = {

'北京':{

'朝阳':{

'朝阳1':{},

'朝阳2':{},

'朝阳3':{},

},

'丰台':{

'丰台1':{},

'丰台2':{},

'丰台3':{},

},

'通州':{

'通州1':{},

'通州2':{},

'通州3':{},

},

},

'河南':{

'郑州':{

'郑州1':{},

'郑州2':{},

'郑州3':{},

},

'洛阳':{

'洛阳1':{},

'洛阳2':{},

'洛阳3':{},

},

'周口':{

'周口1':{},

'周口2':{},

'周口3':{},

},

},

'上海':{

'上海1':{},

'上海2':{},

'上海3':{},

},

}

'''''''''''''''''''''打印'''''''''''''''''''''''''''''''''

#定义一个标志位

back_flag = False

exit_flag = False

#1。先循环打印第一级菜单:

while not back_flag:

for key in mnue:

print(key)

choice1 = input('1》》》》》:').strip()

#2.根据输入的数据,打印第二级菜单:

if choice1 in mnue:

#3.让打印停留在当前级:

while not back_flag and not exit_flag:

for key2 in mnue[choice1]:

print(key2)

choice2 = input('2》》》》').strip()

if choice2 == 'a':

back_flag = True

if choice2 == 'q':

exit_flag = True

if choice2 in mnue[choice1]:

while not back_flag and not exit_flag:

for key3 in mnue[choice1][choice2]:

print(key3)

choice3 = input('3>>>>>>>>').strip()

if choice3 == 'a':

back_flag = True

if choice3 == 'q':

exit_flag = True

if choice3 in mnue[choice1][choice2]:

while not back_flag and not exit_flag:

for key4 in mnue[choice1][choice2][choice3]:

print(key4)

print('已经是最后一层了')

choice4 = input('如果返回上一层,请输入a')

if choice4 == 'a':

back_flag = True

if choice4 == 'q':

exit_flag = True

else:

back_flag = False

else:

back_flag = False

else:

back_flag = False

方法二:

mnue = {

'北京':{

'朝阳':{

'朝阳1':{},

'朝阳2':{},

'朝阳3':{},

},

'丰台':{

'丰台1':{},

'丰台2':{},

'丰台3':{},

},

'通州':{

'通州1':{},

'通州2':{},

'通州3':{},

},

},

'河南':{

'郑州':{

'郑州1':{},

'郑州2':{},

'郑州3':{},

},

'洛阳':{

'洛阳1':{},

'洛阳2':{},

'洛阳3':{},

},

'周口':{

'周口1':{},

'周口2':{},

'周口3':{},

},

},

'上海':{

'上海1':{},

'上海2':{},

'上海3':{},

},

}

current_lary = mnue

current_list = []

while True:

for key in current_lary:

print(key)

choice = input("请输入所选项")

if len(choice) == 0:

continue

if choice in current_lary:

current_list.append(current_lary)

current_lary = current_lary[choice]

elif choice == 'b':

if current_list:

current_lary = current_list.pop()

else:

print("结束")

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值