需求:购物车程序 购买这些东西"iphone","coffee","book","cup" 对应的价格是4200,30,50,300 买不起,打印差多少钱 放入购物车,扣钱,同时打印余额 选择不买了,打印购买的商品列表、价格和余额
Products=["iphone","coffee","book","cup"]#商品列表 Price=[4200,30,50,300] #产品价格表 Salary=int(input("input your salary:")) #输入工资 count=0 #统计买了的商品数量 Your_Shopping_Cart=[ ] #购买了的商品列表 Your_Price=[] #购买了的商品价格列表 Your_Price_Totel = 0 #购买了的商品总价 while True: print("序号", "商品", "价格") for a in range(0, len(Products)): print("%d %-7s %s"%(a + 1,Products[a], Price[a])) Choice = input(">>:").strip() if Choice.isdigit():#判断输入是否为整数 Choice = int(Choice) #选择购买的商品 if Choice >=1 and Choice <= len(Products): if Price[Choice-1]<= Salary: Your_Shopping_Cart.append(Products[Choice - 1]) # 将购买的商品名字追加进购买了的商品列表 Your_Price.append(Price[Choice - 1]) # 将选择的商品元素的价格追加进购买了的商品价格列表 Salary -= Price[Choice-1] print("你选的商品"+Products[Choice-1]+"已放入购物车,你的余额为:",Salary) #统计出余额剩多少 else: print("余额不够,商品价格是"+str(Price[Choice-1])+",你还差"+str(Price[Choice-1]-Salary)) else: print("商品不存在!") elif Choice == "no": print("-------以购买的商品列表--------") print("%s %8s %8s %8s %8s"%("序号", "商品", "数量", "单价", "总价")) New_Your_Shopping_Cart=[] # for a in Your_Shopping_Cart: # if a not in New_Your_Shopping_Cart: # New_Your_Shopping_Cart.append(a) #购物车去重 New_Your_Price=[] for a in Your_Price: if a not in New_Your_Price: New_Your_Price.append(a) #价格表去重 for a in New_Your_Shopping_Cart: index=0 print("%-10s %-13s %-8s %-13s %-12s"%(index+1,a,Your_Shopping_Cart.count(a),New_Your_Price[index],Your_Shopping_Cart.count(a)*New_Your_Price[index]))#打印出购买了的商品和价格 index+=1 print("你的余额为:", Salary) break else: print("无此选项!")
时间: 2024-10-12 19:58:50