利用字典和列表实现城市多级菜单
#coding:utf-8
#利用字典和列表实现城市多级菜单
addrIndex = {"":"北京","":"上海","":"南京","":"福建"}
addrDict = {"北京":["东城区","西城区","大兴区"],"上海":["黄浦区","长宁区","宝山区"],"南京":["玄武区","秦淮区","六合区"],"福建":["龙岩","泉州"]}
while True:
i = 1
#循环打印城市名称
for cs in addrDict.keys():
print("%d.%s" %(i,cs))
i += 1
choIndex = input("请输入你城市编号:") #为每个城市指定编号值,便于判断输入错误和扩展
if choIndex not in addrIndex.keys():
print("输入有误!")
continue
# print(addrDict[addrIndex[choIndex]]) while True:
i = 1
#根据输入的城市编号,获取城市名称,城市名称直接为addrDict字典的key
for pq in addrDict[addrIndex[choIndex]]:
print("%d.%s" %(i,pq))
i += 1
pqCho = input("请输入片区编号,输入q返回上级:") #输入q跳出
if pqCho == "q":
break
#输入正确的值打印,pqCho.isdigit()用于判断输入非q字符串错误
elif pqCho.isdigit() and (int)(pqCho)-1>=0 and (int)(pqCho)-1<=len(addrDict[addrIndex[choIndex]])-1:
print("你选择的是:%s" %(addrDict[addrIndex[choIndex]][(int)(pqCho)-1]))
else:
print("输入有误!")
continue