Python3實現購物車程序_高級
阿新 • • 發佈:2018-06-04
ice name 電腦 code usr AR IT lower color
#!usr/bin/env python # -*- coding:utf-8 -*- assets = 0 li = input("請輸入總資產:") assets = li goods = [ {"name": "電腦", "price": 2000}, {"name": "遊艇", "price": 3000}, {"name": "手機", "price": 1900}, {"name": "美女", "price": 4000}, {"name": "手表", "price": 1000}, ] for i in goods: print(i["name"],i["price"]) car_dict = {} while True: i2 = input("請選擇您需要購買的商品:") if i2.lower() == "y": break for itemd in goods: if itemd["name"] == i2: name = itemd["name"] if name in car_dict.keys(): car_dict[name]["num"] = car_dict[name]["num"] + 1 else: car_dict[name] = {"num": 1,"single_price": itemd["price"]} print(car_dict) #結算 car_all_price = 0 for k,v in car_dict.items(): n = v["single_price"] m = v["num"] car_all_price_sum = n * m car_all_price = car_all_price + car_all_price_sum print(assets,car_all_price) ifint(car_all_price) > int(assets): print("窮鬼!") else: print("購買成功,咱回家!")
Python3實現購物車程序_高級