python之三級菜單

python之三級菜單python

要求:ide

1. 運行程序輸出第一級菜單spa

2. 選擇一級菜單某項,輸出二級菜單,同理輸出三級菜單3d

3. 菜單數據保存在文件中code

4. 讓用戶選擇是否要退出blog

5. 有返回上一級菜單的功能utf-8

 

流程圖:input

代碼:it

 

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 
 5 district_maps = {           #定義字典
 6     "華東區":{"山東":["濟南","青島","淄博"],"江蘇":["蘇州","南京","無錫"],"浙江":["杭州","寧波","溫州"],},
 7     "華南區":{"廣東":["廣州","深圳","珠海"],"廣西":["桂林","柳州","北海"],"海南":["三亞","海口","三沙"]},
 8     "華中區":{"湖北":["武漢","宜昌","黃岡"],"湖南":["長沙","株洲","湘潭"],"江西":["南昌","九江","上饒"]},
 9     "華北區":{"北京":["東城區","朝陽區","海淀區"],"天津":["和平區","河西區","河北區"],"河北":["石家莊","唐山","張家口"]},
10     "西北區":{"寧夏":["銀川","吳忠","固原"],"青海":["西寧","海東","海北藏族自治州"],"陝西":["西安","咸陽","延安"]},
11     "西南區":{"四川":["成都","綿陽","廣元"],"雲南":["昆明","麗江","玉溪"],"貴州":["貴陽","安順","銅仁"]},
12     "東北區":{"遼寧":["瀋陽","大連","鞍山"],"吉林":["長春","吉林","遼源"],"黑龍江":["哈爾濱","齊齊哈爾","牡丹江"]},
13     "港澳臺區":{"香港":["香港島","九龍半島","新界"],"澳門":["澳門半島","離島","路氹城"],"臺灣":["臺北","臺中","臺南"]},
14 }
15 
16 def menu():
17    while True:
18         on = ()
19         c = ()
20         for i in district_maps:
21             print(i)
22         on = input("請輸入地區:")
23         while True:
24             if on in district_maps.keys():
25                 for e in district_maps[on]:
26                     print(e)
27                 c = input("請輸入省份:")
28                 if c in district_maps[on]:
29                     for d in district_maps[on][c]:
30                         print(d)
31                     while True:
32                         d = input("返回上層請輸入b,退出請輸入q:")
33                         if d == "b":
34                             break
35                         elif d == "q":
36                             print("謝謝使用")
37                             exit(0)
38                         else:
39                             print("輸入錯誤,請從新輸入")
40                 elif c == "b":
41                     break
42                 elif c == "q":
43                     print("謝謝使用")
44                     exit(0)
45                 else:
46                     print("輸入有誤,請從新輸入")
47             elif on == "q":
48                 print("謝謝使用")
49                 exit(0)
50             else:
51                 print("輸入有誤,請從新輸入")
52 if __name__ == '__main__':
53     menu()
三級菜單
相關文章
相關標籤/搜索