标签:append blocks 多层 用户 span 运行 ima pytho inf
优化的思路是使用单层循环嵌套完成三级菜单,这个优化思路我非常喜欢,我喜欢在编程的时候用最少的东西写出同样的效果,通常这样会绕来绕去,但非常有趣!!!
需求:
1、运行程序输出第一级菜单;
2、选择一级菜单某项,输出二级菜单,同理输出三级菜单;
3、让用户选择是否要退出;
4、有返回上一级菜单的功能;
多层循环嵌套:
data = { ‘A‘:{ "Aa":[‘Aa1‘,‘Aa2‘,‘Aa3‘], "Ab":[‘Ab1‘,‘Ab2‘,‘Ab3‘], "Ac":[‘Ac1‘,‘Ac2‘,‘Ac3‘] }, ‘B‘:{ "Ba":[‘Ba1‘,‘Ba2‘,‘Ba3‘], "Bb":[‘Bb1‘,‘Bb2‘,‘Bb3‘], "Bc":[‘Bc1‘,‘Bc2‘,‘Bc3‘] }, ‘C‘:{ "Ca":[‘Ca1‘,‘Ca2‘,‘Ca3‘], "Cb":[‘Cb1‘,‘Cb2‘,‘Cb3‘], "Cc":[‘Cc1‘,‘Cc2‘,‘Cc3‘] } } jump = True #跳出循环直至退出程序 print("特别提醒:选‘q’退出;选‘b’返回上一级菜单!!") while jump == True: for i in data: print(i) choice = input("请选择进入:") if choice in data: while jump == True: for i1 in data[choice]: print(i1) choice1 = input("请选择进入:") if choice1 in data[choice]: while jump == True: for i2 in data[choice][choice1]: print(i2) choice2 = input("请选择退出或返回上一菜单:") if choice2 == ‘q‘: jump = False elif choice2 == ‘b‘: break else: print("选择错误请重新选择:") elif choice1 == ‘q‘: jump = False elif choice1 == ‘b‘: break else: print("选择错误请重新选择:") elif choice == "q": jump = False else: print("选择错误请重新选择:") print ("退出程序...")
单层循环嵌套:
data = { ‘A‘:{ "Aa":[‘Aa1‘,‘Aa2‘,‘Aa3‘], "Ab":[‘Ab1‘,‘Ab2‘,‘Ab3‘], "Ac":[‘Ac1‘,‘Ac2‘,‘Ac3‘] }, ‘B‘:{ "Ba":[‘Ba1‘,‘Ba2‘,‘Ba3‘], "Bb":[‘Bb1‘,‘Bb2‘,‘Bb3‘], "Bc":[‘Bc1‘,‘Bc2‘,‘Bc3‘] }, ‘C‘:{ "Ca":[‘Ca1‘,‘Ca2‘,‘Ca3‘], "Cb":[‘Cb1‘,‘Cb2‘,‘Cb3‘], "Cc":[‘Cc1‘,‘Cc2‘,‘Cc3‘] } } list_menu = [] flag = True while flag: if len(list_menu) == 0: info = data else: info = list_menu[-1] for i in info: print (i) choice = input("请选择进入:").strip() if choice == ‘q‘: break if choice == ‘b‘: if len(list_menu) == 0: print("已经是最高级菜单!!") continue list_menu.pop() continue if len(list_menu) == 0: list_menu.append(info[choice]) continue list_menu.append(list_menu[-1][choice]) print("退出程序...")
标签:append blocks 多层 用户 span 运行 ima pytho inf
原文地址:http://www.cnblogs.com/liyongbin/p/6561629.html