1 '''
  2 针对列表names=[‘金角大王’, ‘黑姑娘’, ‘rain’, ‘eva’, ‘狗蛋’, ‘银角大王’, ‘eva’,’鸡头’]进入以下操作
  3 
  4 
  5 1.通过names.index()的方法返回第2个eva的索引值
  6 
  7 2.把以上的列表通过切片的形式实现反转
  8 
  9 3.打印列表中所有下标为奇数的值
 10 
 11 4.通过names.index()方法找到第2个eva值 ,并将其改成EVA
 12 '''
 13 '''
 14 names=['金角大王', '黑姑娘', 'rain', 'eva', '狗蛋', '银角大王', 'eva','鸡头']
 15 #1.
 16 # print(names.index('eva',names.index('eva')+1))
 17 #2.
 18 # names.reverse()
 19 # print(names)
 20 #3.
 21 print(names[1::2])
 22 #4.
 23 names[6] = names[names.index('eva',names.index('eva')+1)].upper()
 24 print(names)
 25 '''
 26 
 27 ######################购物车######################
 28 '''
 29 实现功能要求:
 30 1、启动程序后,让用户输入工资,然后进入循环,打印商品列表和编号
 31 2、允许用户根据商品编号选择商品
 32 3、用户选择商品后,检测余额是否够,够就直接扣款,并加入购物车, 不够就提醒余额不足
 33 4、可随时退出,退出时,打印已购买商品和余额
 34 '''
 35 
 36 from collections import Counter
 37 
 38 goods = [
 39 {"name": "电脑", "price": 1999},
 40 {"name": "鼠标", "price": 10},
 41 {"name": "游艇", "price": 20},
 42 {"name": "美女", "price": 9998},
 43 ]
 44 
 45 # goods_new = {}
 46 v1 = []
 47 names=[] #商品的名字
 48 price= []#商品的价格 1999 10 20 9998
 49 for i in range(4):
 50     for v in goods[i].values():
 51         v1.append(v)
 52         # goods_new.setdefault("商品"+str(i),v1)    ??????????
 53         # print("商品{0:<13}号 {1:^15} ".format(i,v))
 54 for i in v1[::2]:
 55     names.append(i)
 56 for i in v1[1::2]:
 57     price.append(i)
 58 #上面是构造商品数据
 59 salary = int(input("input salary: "))#
 60 print("商品列表".center(60,"*"))
 61 print("{0:^15}{1:^32}{2:>8}(元)".format("num","name","price"))
 62 for i in range(4):
 63     print("{0:^15}{1:^31}{2:^15}".format(i,names[i],price[i]))
 64 
 65 #购买
 66 buy_sign = False
 67 buy_car = {}
 68 while not buy_sign:
 69     try:
 70         choice =input('请输入要购买商品的编号,用逗号隔开:')
 71         num = [int(i) for i in choice.split(',') ]
 72         num =[i for i in num if i < 4]
 73         num_dic = dict(Counter(num))#把购买的商品序号包装成字典
 74         # num_dic = sorted(num_dic.items(),key=lambda dic:dic[0])#排序后转换成列表
 75         cost =0
 76         for k,v in num_dic.items():
 77             print("您选择了{0}号商品,{1},数量是{2},单价是{3}元".format(k,names[k],v,price[k]))
 78             buy_car.setdefault(names[k],"数量:"+str(v))
 79             # print("buy_car: ",buy_car)
 80             cost += v * price[k]
 81         print("总价是:{}元,你的工资是:{}元".format(cost,salary))
 82 
 83         buy_choice = input("y或Y确认购买,q或Q退出: ")
 84         buy_choice = buy_choice.lower()
 85         if buy_choice == 'y' :
 86             if cost <= salary:
 87                 print("购买成功,余额为{}元".format(salary-cost))
 88                 for k, v in buy_car.items():
 89                     print("已经购买{0},{1}".format(k, v))
 90             else:
 91                 print("工资不足,请重新选择")
 92                 num_dic.clear()
 93                 buy_car.clear()
 94         elif buy_choice == 'q':
 95             break
 96         else:
 97             print("输入错误")
 98 
 99         if num_dic:
100             buy_sign =True
101         # print(num_dic)
102     except ValueError:
103         print("输入错误,请重新输入.")
104 # print(num)

 

posted on 2019-07-05 17:39  嗯哈哈  阅读(350)  评论(0编辑  收藏  举报