要求:
1)实现三级菜单
2)可依次进入各子菜单
3)所需知识列表、字典
程序可实现功能:
1)在实现上述三个要求的基础上,对程序运行过程异常进行处理:包括输入异常处理(输入为空直接退出程序),无菜单层数的限制,对进入底层菜单和顶层菜单进行了特殊处理(防止溢出等异常)
输入为:
'''
Created on
@author: hcl
'''
China_city = {
'北京':{
'朝阳':{'国贸','CBD','天阶'},
'海淀':{'圆明园 ','苏州街','中关村'},
'昌平':{'沙河','南口','小汤山'}
},
'江苏':{
'南京':{'浦口','江宁','鼓楼'},
'苏州':{'吴中','吴江','苏州园林'},
'扬州':{'瘦西湖','都江','123'}
}
}
China_city_temp = China_city.copy()
input_history = []
while True: #循环打印当前目录
for i in China_city_temp:
print(i) #保证不输入或者输入空格 程序异常
try:
input_cityName = input("Please input your choise:").strip()
except:
break
#输入分类 #输入q 退出
if input_cityName == 'q':
break
#输入b 返回上一级
elif input_cityName == 'b':
try:
input_history.pop()
#print(input_history,input_history[-1])
except:
input_history = []
print("返回到底了")
#进入当前目录内容 进入下一级
elif input_cityName in China_city_temp:
if len(input_history) < len(China_city):
input_history.append(input_cityName)
#输入错误 打印提示信息
else:
print("Please input the right name above the list....")
continue
# print(len(input_history))
# print(China_city_temp)
temp_list = China_city.copy()
for i in input_history:
#print(i)
#China_city_temp = China_city[i]
temp_list = temp_list[i]
China_city_temp = temp_list
输出为:
北京
江苏
Please input your choise:北京
len(input_history):0 len(China_city):2
朝阳
海淀
昌平
Please input your choise:朝阳
len(input_history):1 len(China_city):2
国贸
天阶
CBD
Please input your choise:国贸
len(input_history):2 len(China_city):2
国贸
天阶
CBD
Please input your choise:b
朝阳
海淀
昌平
Please input your choise:朝阳
len(input_history):1 len(China_city):2
国贸
天阶
CBD
Please input your choise:
六、有兴趣接电子设计相关小型项目的请加下群,每个项目一般在1000元以内,非诚勿扰