1. 程式人生 > >第一周作業-三級菜單

第一周作業-三級菜單

menu 轉換 inpu print enc 關閉 是否 () 函數


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


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


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


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


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


1
#coding:utf-8 2 #Author:Mr Zhi 3 file_o = open("menu",r,encoding="utf-8") #打開三級菜單文件 4 menu= eval(file_o.read()) #把打開後的文件內容轉換成字典(文件內容原本是字典) 5 def out(): #頻繁使用的代碼定義個函數 6 file_o.close() #關閉打開的文件
7 exit() #退出程序 8 for i in menu: #遍歷菜單menu字典 9 print(i) #輸出字典菜單 10 while True: #開始while循環 11 choice = input("輸入地區(按q退出,b返回上層):") #輸入地區 12 if choice == "q": #判斷輸入chice值是否為q 13 out() #執行函數 14 if choice == "b": #判斷輸入chice值是否為b 15 print("已是第一層") 16 if choice not
in menu or len(choice) == 0 and choice != "b": #判斷輸入choice值在菜單文件中或輸入choice不等於b 17 print("你輸入有誤,請重新輸入") 18 if choice in menu: #p判斷輸入choice是否在menu中 19 for i in menu[choice]: #遍歷menu[choice] 20 print(i) 21 choice2 = input("輸入省區(按q退出,b返回上層):") #輸入結果 22 if
choice2 not in menu[choice] and choice2 != "b":#判斷輸入choice2 不在字典中和不等於b 23 print("你輸入有誤,請重新輸入") 24 if choice2 in menu[choice]: #判斷 25 for i2 in menu[choice][choice2]: #遍歷字典 26 print(i2) 27 print("最後一層菜單,下面沒有了") 28 if choice2 == "q": #判斷 29 out() 30 if choice2 == "b":#判斷 31 continue #繼續循環

第一周作業-三級菜單