听取了Alex老师的话,程序里面尽量用英文,本人编程小白,英语菜鸟,英语不会的地方有用中文表示了,显得代码有些乱,不足之处请多多指教!
catalog = { #在此定义一个三级菜单 "food":{ "fruit":["apple","pear","litchi","banana","watermelon"], "vegetable":["potato","pumpkin","leek","spinach","cowpea"], "dessert":["cake","cookie","chocolate","butter","ice cream"]}, "furniture":{"electric":["television","washing machine","freezer","microwave oven"], "wood":["door","table","bed","cabinet","safa"], "vehicle":["bicycle","motorcycle","car"]}, "year":{"season":["spring","summer","autumn","winter"], "month":["January","February","March","April","May","June","July","August","September","October","November","December"], "festival":["spring festival","april fool's day","Labour Day","valentine's say","Christmas"] } } print("+-------------------------+") #打印菜单选项栏 print("+---see the catalog: C---+") print("+---quit the catalog: Q---+") print("+---Back up : B---+") print("+-------------------------+") temp = True #定义一个用于可随时终止循环的变量 def firt_catalog(first_level): #定义一级目录菜单选项格式
if first_level.lower() == "q": #系统在打印了根目录时,用户输入Q,则直接返回False,将false传递while循环的temp中,终止循环退出程序 return False
elif first_level.lower() == "b": #用户输入B之后,返回上一层菜单,因为最初为根目录。打印提示根目录 print("已经是根目录了!") return False else: #如果用户输入的是一级菜单的选项,则打印对应的二级菜单 for each2 in catalog[first_level]: print(each2 + ",", end="") print("\nselect level 2 catalog or other choose:") second_level = input("pleace choose:") #用户做出选择 second_catalog(first_level,second_level) #跳转到二级菜单选择函数中,并将用户选择结果传递到二级菜单选项函数 def second_catalog(first,second): #定义二级菜单选项函数 if second.lower() == "q": return False elif second.lower() == "b": #如果用户输入B,则返回上级目录 for each in catalog: #打印上级目录 print(each + ",", end="") print("\nselect level 1 catalog or other choose:") first_level = input("pleace choose:") firt_catalog(first_level) #打印完上级目录之后,因为用户又返回到了一级目录,所以此处又去调用一级目录菜单选择函数 else: #如果用户输入二级目录菜单的元素,则打印该元素下的三级目录菜单 for each3 in catalog[first][second]: print(each3 + ",", end="") print("\nselect level 3 catalog or other choose:") third_choose = input("pleace choose:") third_catalog(first,second,third_choose) #进入三级目录菜单选择函数 def third_catalog(first,second,third): #定义一个三级目录菜单 if third.lower() == "q": return False elif third.lower() == "b": for each2 in catalog[first]: print(each2 + ",", end="") print("\nselect level 2 catalog or other choose:") second_level = input("pleace choose:") second_catalog(first,second_level) else: return False user_choose = input("pleace choose:") #用户根据系统提示的选项卡做出选择 while temp: #此处用一个变量作为循环的判断条件,当用户退出后,或者选择了第三级目录菜单则直接修改变量值,用于终止循环 if user_choose.lower() == "c": for each in catalog: print(each + ",", end="") print("\nselect level 1 catalog or other choose:") first_level = input("pleace choose:") temp = firt_catalog(first_level) elif user_choose.lower() == "b": print("已经是根目录了!") temp = False else: temp = False