1. 程式人生 > >python 購物車作業

python 購物車作業

程式:購物車程式

需求:

啟動程式後,讓使用者輸入工資,然後列印商品列表
允許使用者根據商品編號購買商品
使用者選擇商品後,檢測餘額是否夠,夠就直接扣款,不夠就提醒
可隨時退出,退出時,列印已購買商品和餘額'''
shopping_list = []
shopping_goods = [
("bicycle",500),
("car",5000),
("computer",8000),
("liwei",88),
("liqui",66),
("xiaojun",664),
]
salary = input("請輸入您的工資:")
if salary.isdigit():
salary = int(salary)
while True:
for i in shopping_goods:
print(shopping_goods.index(i),i)
chouse = input("請選擇您需要的商品編號:")
if chouse.isdigit():
chouse = int(chouse)
if chouse >= 0 and chouse < len(shopping_goods):
p_itme = shopping_goods[chouse]
if p_itme[1]<=salary: #餘額足夠買得起商品
salary -= p_itme[1]
shopping_list.append(p_itme)
print("您已購買的商品為:%s"%shopping_list,"您的餘額還剩下:%s"%salary)
else:
print("您的餘額不足")
print("您已經買的商品為:",shopping_list)
break
else:
print("您選擇的商品不存在,請從新選擇。")
else:
chouse == "eeeeeee"
print("您所購買的商品為:", shopping_list,)
print("您的餘額還剩下:%s"%salary)
break