Python第一週做業--三級菜單

循環用的多,之後再優化吧python

三級菜單:ide

  1. 運行程序輸出第一級菜單
  2. 選擇一級菜單某項,輸出二級菜單,同理輸出三級菜單
  3. 菜單數據保存在文件中
  4. 讓用戶選擇是否要退出
  5. 有返回上一級菜單的功能

data.txt 保存數據文件優化

{'河北':{ "石家莊":{"長安區":["oldboy","baidu","tencent"],"橋東區":["百事可樂","雪碧","芬達"]}, "邯鄲":{ "叢臺區":["新世紀","天鴻"]}},
    '北京':{"西城區":{"宣武門":["錦江之星","漢庭","如家"],"廣安門":["希爾頓","速8","7天"]}}}

menu.py 運行文件code

#!/use/bin/env python
# -*- coding:utf-8 -*-

f = open("data.txt","r",encoding="utf8")
f=f.read()
data=eval(f) # 把文本中內容轉成字典

while True:
    for line in data:
        print(line)
    choice = input("選擇進入1:")
    if choice in data:
        while True:
            for line2 in data[choice]:
                print("-->",line2)
            choice2 = input("選擇進入2:")
            if choice2 in data[choice]:
                while True:
                    for line3 in data[choice][choice2]:
                        print("---->",line3)
                    choice3 = input("選擇進入3:")
                    if choice3 in data[choice][choice2]:
                            for line4 in data[choice][choice2][choice3]:
                                print(line4)
                            choice4 = input("最後一層,返回請按b,退出請按q:")
                            if choice4 == "b":
                                pass
                            elif choice4 == "q":
                                exit()
                    elif choice3 == "b":
                        break
                    elif choice3 == "q":
                        exit()
            elif choice2 == "b":
                break
            elif choice2 == "q":
                exit()
相關文章
相關標籤/搜索