简单按照老师的思路做了一个三级菜单,感觉最有意思的是用了三个循环点,i=1,i=2,i=3,然后程序可以跳转到相应的循环点去,程序如下:
1 #!/usr/bin/env python3 2 #-*_coding:utf-8_*_ 3 city_overview={'peking':{'peking':['estern','western']},'shanghai':{'shanghai':['pudong','hongkou','fengxian']}} 4 5 print('now you can choose the city:',city_overview.keys()) 6 i=1 7 while i==1: 8 prov=input('Please input input a prov(press "b" to go back):') 9 if prov in city_overview.keys(): 10 i = 2 11 while i==2: 12 for i in city_overview[prov].keys(): 13 print(i) 14 city=input('please choose a city(press "b" to go back):') 15 if city in city_overview[prov].keys(): 16 i=3 17 while i==3: 18 for i in city_overview[prov][city]: 19 print(i) 20 country=input('Please input the country(press "b" to go back):') 21 if country in city_overview[prov][city]: 22 print(prov,city,country) 23 break 24 elif country=='b' or country=='B': 25 i=2 26 break 27 else: 28 print('invalid country input,please try again') 29 elif city == 'b' or city == 'B': 30 i = 1 31 break 32 else: 33 print('invalid city input,please try again.') 34 elif prov=='b' or prov=='Q': 35 break 36 else: 37 print('invalid province input,please try again.')