1. 程式人生 > >python簡單購物車

python簡單購物車

# Author:Zhu Jin Xin
product_list =[
("Airpos",1200),
("Beats",2500),
("Iphone7",4800),
("Iphone8",5800),
("IphoneX",9800),
("IphoneXS",10800)
]
shopping_list = []
salary = input("輸入你的工資:")
if salary.isdigit():
salary = int(salary)
while True:
for index,item in enumerate(product_list):
print(index,item)
user_choice =input("輸入你想購買商品的編號,退出請按q:")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice<len(product_list) and user_choice>=0:
p_item = product_list[user_choice]
if p_item[1]<=salary:
salary -=p_item[1]
shopping_list.append(p_item)
print("購買%s成功,餘額為:\033[31;1m%s\033[0m"%(p_item,salary))
else:
print("\033[31;1m你的餘額只剩%s啦\033[0m"%salary)
else:
print("\033[31;1m你輸入的%s號商品不存在,請重新選擇\033[0m"%user_choice)
elif user_choice =="q":
print("--------shopping list--------")
for p in shopping_list:
print(p)
print("你的餘額為:",salary)
exit()
else:
print("無效輸入,請重新輸入")
else:
print("工資格式錯誤,必須為數字請重新輸入")
import hhhe