1. 程式人生 > >Python 購物車

Python 購物車

ppi pytho bold 技術分享 顯示 play logs isdigit bsp

功能要求:

要求用戶輸入總資產,例如:2000
顯示商品列表,讓用戶根據序號選擇商品,加入購物車
購買,如果商品總額大於總資產,提示賬戶余額不足,否則,購買成功。
附加:可充值、某商品移除購物車
goods = [
{"name": "電腦", "price": 1999},
{"name": "鼠標", "price": 10},
{"name": "遊艇", "price": 20},
{"name": "美女", "price": 998},
]

技術分享
shoping=[]

goods = [
    {"name": "電腦", "price": 1999},
    {"name"
: "鼠標", "price": 10}, {"name": "遊艇", "price": 20}, {"name": "美女", "price": 998}, ] while True: salary=input(Your salary : ) if salary.isdigit(): int(salary) break else: continue while True: # print(goods) for index , i in enumerate(goods):
print(index , . , i["name"],i["price"] ) choice = input(請選擇要購買的商品編號:) salary=int(salary) if choice.isdigit(): choice=int(choice) if choice >=0 and choice < len(goods): p=goods[choice] # print(p) if salary >=p[price]: salary
-= p["price"] shoping.append(p) print("Added \033[32;1m[%s]\033[0m into you shopping cart,and you current balance is \033[31;1m%s\033[0m " % ( p[name], salary)) else: print("你的錢不夠") chongzhi=input(是否需要充值 Y/N:) if chongzhi == "Y" or chongzhi == y : salary_chongzhi=input("請輸入充值的金額") if salary_chongzhi.isdigit(): salary_chongzhi=int(salary_chongzhi) salary=salary_chongzhi+salary else: continue elif choice == delete: for index, i in enumerate(shoping): print(index, ., i[name], i[price]) delete = input(請輸入你想要刪除的商品編號:) if delete.isdigit(): delete=int(delete) # if delete >=0 and delete < len(shoping): salary+=shoping[delete][price] shoping.pop(delete) elif choice == shop: print(shoping) elif choice=="quit": print("你購買的商品列表:",shoping) exit()
View Code

 

Python 購物車