# 三级菜单; # 省、市、县 可随时返回上一级(b),可以随时退出(q); add_dest={'jiangsu': {'wuxi': ['宜兴', 'jiangying', 'xishan'], 'nanjing': ['qixia', 'gulou', 'changning', 'zhongshan']}, 'zhejiang': {'嘉兴': ['a2', 'a3', 'a4'],'宁波': ['a1', 'a2', 'a3']}, 'guangzhou': {1: ['a'], 2: ['b'], 3: ['c']} } break_flag = Falsewhile break_flag == False: print("可选择省份:") for i in add_dest: print(i) pick_add = input("选择省份:") if pick_add == 'q': break while pick_add in add_dest and break_flag == False: print("可选择的市:") for k in add_dest[pick_add]: print(k) pick_add2 = input("选择市:") if pick_add2 in add_dest[pick_add]: print("您的目的地是:%s"%pick_add2) elif pick_add2 == 'b': break elif pick_add2 == 'q': break_flag = True break while pick_add2 in add_dest[pick_add]: print("可选择的县:") for j in add_dest[pick_add][pick_add2]: print(j) pick_add3 = input("最终目的地:") if pick_add3 in add_dest[pick_add][pick_add2]: print("您的最终目的地是:%s"%pick_add3) pick_add4 = input("按b返回,按q退出:") if pick_add4 == 'b': continue if pick_add4 == 'q': exit() elif pick_add3 == 'b': break elif pick_add3 == 'q': break_flag = True break
KEY菜单用while循环:好处是输入有误自动再次循环;如果用for循环key 需要判断几个其他情况的条件;
value处用for循环
break_flag用于熟悉使用;
三级菜单优化:不同之处:最后一级是字典,(原最后一级是数组;)
menu={'jiangsu': {'wuxi': {'宜兴':{'官林':{}}, 'jiangying':{}, 'xishan':{}}, 'nanjing': {'qixia', 'gulou', 'changning', 'zhongshan'}}, 'zhejiang': {'嘉兴': {'a2', 'a3', 'a4'},'宁波': {'a1', 'a2', 'a3'}}, 'guangzhou': {1: {'a'}, 2: {'b'}, 3:{'c'}} } last_layers=[menu] current_layer = menu while True: for i in current_layer: print(i) choice = input(">>:").strip() if len(choice)==0:continue if choice in current_layer: last_layers.append(current_layer) # print (last_layers) current_layer = current_layer[choice] #下一层; print (current_layer) if choice == 'b': if last_layers: current_layer = last_layers[-1] last_layers.pop()
购物车优化:
your_cart=[] cart={} product_list=[['iphone',5000],['book',50],['bike',1500],['coffee',30],['ticket',80]] salary = input('pls input your salary:') ct = 1 if salary.isdigit(): salary = int(salary) # 如果输入的是0-9字符串类型的数字;(它是数字,只不过是字符串形式)那么认为是真!然后转int类型; while True: for i in enumerate(product_list): print(i) your_choice = input("pls pick your favorite stuff:") if your_choice.isdigit(): your_choice = int(your_choice) if your_choice <len(product_list) and your_choice >=0: product = product_list[your_choice] print(product) if product_list[your_choice][1]<=salary: if product_list[your_choice][0] in cart: cart[product[0]][1] += 1 #计数 salary = salary - product_list[your_choice][1] print("in your_cart:%s,and money left:%s"%(cart,salary)) else: cart[product[0]] = [product[1],1] # print(cart) salary = salary - product_list[your_choice][1] print("in your_cart:%s,and money left:%s"%(cart,salary)) else: print("not enough money!") else: print("your choice is not exit.") continue elif your_choice == 'q': print("序号:","商品:","单价:","数量:","单项总价:")
totalcost=0 for i in (cart): print(ct,i,cart[i][0],cart[i][1],cart[i][1]*cart[i][0]) ct +=1
totalcost +=cart[i][1]*cart[i][0] print("总花费:%s"%totalcost) break # elif your_choice == "\n\t": #python遇到回车默认结束! # print("in your_cart:%s,and money left:%s"%(cart,salary)) # # elif your_choice == 'manager': # print("welcome manager") # add_product = input("goods %s,price %s"%(goods,price))