python三级菜单实例_python三级菜单实例(傻瓜版和进阶版)

程序: python三级菜单

要求: :

1.打印省、市、县三级菜单

2.可返回上一级

3.可随时退出程序

方案一:傻瓜版(其实傻瓜版考察的主要是思路!思路清楚了,那才不是傻瓜!O(∩_∩)O哈哈~)

menu = {

'北京':{

'海淀':{

'五道口':{

'soho':{},

'网易':{},

'google':{}

},

'中关村':{

'爱奇艺':{},

'汽车之家':{},

'youku':{},

},

'上地':{

'百度':{},

},

},

'昌平':{

'沙河':{

'老男孩':{},

'北航':{},

},

'天通苑':{},

'回龙观':{},

},

'朝阳':{},

'东城':{},

},

'上海':{

'闵行':{

"人民广场":{

'炸鸡店':{}

}

},

'闸北':{

'火车战':{

'携程':{}

}

},

'浦东':{},

},

'山东':{},

}

while True:

for i in menu:

print(i)

choice = input("请选择进入1>>:") # 第一级

if choice in menu:

while True:

for i2 in menu[choice]:

print("\t",i2)

choice2 = input("请选择进入2>>:") # 第二级

if choice2 in menu[choice]:

while True:

for i3 in menu[choice][choice2]:

print("\t\t", i3)

choice3 = input("请选择进入3>>:") # 第三级

if choice3 in menu[choice][choice2]:

for i4 in menu[choice][choice2][choice3]:

print("\t\t\t",i4)

choice4 = input("最后一层,按b返回>>:") # 第四级,主要是为了返回上一层

if choice4 == "b":

pass

if choice3 == "b":

break

if choice2 == "b":

break

方案二:进阶版(虽说是进阶版,但其实改的地方只是代码重复的地方,思路没变,结果也一样)

menu = {

'北京':{

'海淀':{

'五道口':{

'soho':{},

'网易':{},

'google':{}

},

'中关村':{

'爱奇艺':{},

'汽车之家':{},

'youku':{},

},

'上地':{

'百度':{},

},

},

'昌平':{

'沙河':{

'老男孩':{},

'北航':{},

},

'天通苑':{},

'回龙观':{},

},

'朝阳':{},

'东城':{},

},

'上海':{

'闵行':{

"人民广场":{

'炸鸡店':{}

}

},

'闸北':{

'火车战':{

'携程':{}

}

},

'浦东':{},

},

'山东':{},

}

exit_flag = False

current_layer = menu

layers = [menu]

while not exit_flag:

for k in current_layer:

print(k)

choice = input(">>:").strip()

if choice == "b":

current_layer = layers[-1]

#print("change to laster", current_layer)

layers.pop()

elif choice not in current_layer:continue

else:

layers.append(current_layer)

current_layer = current_layer[choice]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值