一.作业需求:
1. 运行程序输出第一级菜单
2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单
3. 菜单数据保存在文件中
4. 让用户选择是否要退出
5. 有返回上一级菜单的功能
二三级菜单文件内容:
{ '北京':{ "昌平":{ "沙河":["oldboy"], "天通苑":["链家地产","我爱我家"] }, "朝阳":{ "望京":["奔驰","陌陌"], "国贸":{"CICC","HP"}, "东直门":{"Advent","飞信"}, }, "海淀":{}, }, '山东':{ "德州":{"扒鸡"}, "青岛":{"啤酒"}, "济南":{"趵突泉"} }, '广东':{ "东莞":{"有小姐姐~~"}, "常熟":{"我也不知道是什么"}, "佛山":{"无影脚"}, }, }
代码:
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author: Colin Yao import sys with open('three','r',encoding='utf-8') as f: file = f.read() for lines in file: f_str =str(file) data = eval(f_str) def list(): for i in data: print(i) def chos1(choice1): while choice1 != 'q': global one one = choice1 if choice1 in data: for i2 in data[one]: print(" ", i2) chos2(input("请输入第二层:")) break else: error = input("输入错误,请重新输入第一层:") if error == 'q': sys.exit() else: chos1(error) else: sys.exit() def chos2(choice2): while choice2 != 'q': global two two = choice2 if choice2 in data[one]: for i3 in data[one][two]: print(" ", i3) chos3(input("请输入第三层:")) break elif choice2 == 'b': list() chos1(input("请输入第一层:")) break else: error2 = input("输入错误,请重新输入第二层:") if error2 == 'q': sys.exit() else: chos2(error2) else: sys.exit() def chos3(choice3): while choice3 != 'q': global three three = choice3 if choice3 in data[one][two]: for i4 in data[one][two][three]: print(" ", i4) chos4(input("最后一层,退回上一层请按‘b’,退出请按'q")) break elif choice3 == 'b': chos1(one) break else: error3 = input("输入错误,请重新输入第三层:") if error3 == 'q': sys.exit() else: chos3(error3) else: sys.exit() def chos4(choice4): while choice4 != 'q': if choice4 == 'b': chos2(two) else: chos4(input("最后一层了,退回上一层请按‘b’,退出请按'q'")) else: sys.exit() print("退出请按'q',返回上级菜单请按'b,第一层不能使用返回'") list() chos1(input("请输入第一层:"))
可以一次循环搞定
待补充ing