python 循环嵌套
需求如下:
1.
1 #定义商品列表类 2 # def shangpin_list(): 3 # for i in com_list: 4 # print(com_list.index(i),end=" ") 5 # for j in i: 6 # print(j,end=" ") 7 # print() 8 # else: 9 # return True 10 11 #程序入口 12 13 salary = int(input("请输入您的工资:")) 14 com_list = [['iphone6s',5000],['mac book',9000],['coffee',32],['python book',80]] 15 shopping_cart_list = [] #购物车 16 cum_money = [] #账单 17 car_list = [] 18 com_count = 0 19 while True: 20 if salary >= 0: 21 for i in com_list: #展示商品 22 print(com_list.index(i),end=" ") 23 for j in i: 24 print(j,end=" ") 25 print() 26 gm = input('''想买点啥? 27 输入商品编号即可购买,输入quit退出商城。 28 请输入》》》''') #开始购物 29 if gm != 'quit': 30 try: 31 gm = int(gm) 32 salary = salary - int(com_list[gm][1]) 33 if salary >= 0: 34 com = com_list[gm][0] #购买的商品 35 com_money = com_list[gm][1] 36 shopping_cart_list.append([com,com_money]) #购买的商品 37 com_count = com_count+com_money 38 print('购买',com,'您需要支付',com_money,'元--->扣除后余额为:%d'%salary) 39 else: 40 print("余额不足,骚年快去挣钱吧!") 41 except ValueError or IndexError as err: 42 print("输入有误呕!") 43 else: 44 print( '本次您购买了以下商品,共计花费:%d元'%com_count) 45 for ij in shopping_cart_list: 46 print( '%s %s元'%((ij[0]),ij[1])) 47 else: 48 exit("您还有余额%d欢迎下次光临"%salary) 49 continue
1 #!/user/bin/env python3 2 # -*- coding=utf-8 -*- 3 #__author__:gq 4 #date:2016/8/24 0024 5 6 dic = {'北京':{'昌平':['沙河','西二旗','西三旗'],'海淀':['圆明园']}, '河北':{'石家庄':['新华区'],'衡水':['老白干']},'广州':{'深圳':['华强北']}} 7 # for index in dic.items(): 8 # print(index) 9 #一级菜单 10 def menu_1(): 11 dic_01={} 12 while True: 13 print('一级菜单'.center(55,'#')) 14 for index,menu_name in enumerate(dic): 15 print(index,menu_name) 16 dic_01.setdefault(index,menu_name) 17 print(dic_01) 18 chose = input('输入菜单序号进入下级菜单,输入b反回上一级菜单') 19 if chose.isdigit(): 20 chose = int(chose) 21 if dic_01.get(chose) == None: 22 print('没有这%s这个菜单项,请重新输入!'%chose) 23 continue 24 else: 25 #print(dic_01.get(chose)) 26 menu_2(dic_01.get(chose)) 27 elif chose == 'q': 28 print("程序退出") 29 else: 30 print("您的输入有误,请重新输入!") 31 32 #二级菜单 33 def menu_2(name): 34 dic_2 = {} 35 while True: 36 flag = False 37 print("%s 二级菜单".center(55,'#')%name) 38 for index,menu in enumerate(dic[name]): 39 print(index,menu) 40 dic_2.setdefault(index,menu) 41 print(dic_2) 42 chose2 = input('输入菜单序号进入下级菜单,输入b反回上一级菜单,输入q退出') 43 if chose2.isdigit(): #判断用户输入的内容是否是数字样式 44 chose2 = int(chose2) 45 #print(dic_2.get(chose2)) 46 if dic_2.get(chose2) == None: 47 print('没有这%s这个菜单项,请重新输入!'%chose2) 48 else: 49 print(dic_2.get(chose2)) 50 flag = menu_3(name,dic_2.get(chose2)) 51 if flag: 52 return True 53 else: 54 continue 55 elif chose2 == 'q': #用户输入‘q'则退出程序 56 print("程序退出") 57 elif chose2 == 'b': #用户输入'b'则反回上级菜单 58 break 59 else: 60 print("您的输入有误,请重新输入!") 61 62 #三级菜单 63 def menu_3(name1,name2): 64 dic_3 ={} 65 while True: 66 print('%s 三级菜单'.center(55,'#')%name2) 67 for index3,menu3 in enumerate(dic[name1][name2]): 68 print(menu3) 69 dic_3.setdefault(index3,menu3) 70 print(dic_3) 71 chose3 = input('输入b反回上一级菜单,输入m反回主菜单,输入q退出') 72 if chose3 == 'q': 73 print("程序退出") 74 exit() 75 elif chose3 == 'm': 76 return True 77 elif chose3 == 'b': 78 return False 79 else: 80 print("您的输入有误,请重新输入!") 81 82 83 while True: 84 menu_1()
1 dic = {'北京':{'昌平':{'沙河':{},'回龙观':{},'巩华城':{}}, 2 '海淀':{'圆明园'}}, 3 '河北':{'石家庄':{'新华区'}, 4 '衡水':{'老白干'}}, 5 '广州':{'深圳':{'华强北'}}} 6 dic1 = dic 7 parent_list=[] 8 while True: 9 for i in dic1: 10 print(i) 11 choose = input('>>>').strip() 12 if len(choose)==0:continue 13 if choose in dic1: 14 parent_list.append(dic1) 15 dic1 = dic1[choose] 16 elif choose == 'b': 17 if parent_list: 18 dic1 = parent_list.pop() 19 else: 20 print("无此项")