product_list = [
('iphone',5000),
('coffee',31),
('bicyle',888),
('iwatch',2666),
('Mac Pro',12000),
('book',88)
] #列表顯示商場商品
shopping_car = [] #用個空列表來充當購物車,
salary = input("請輸入你的金幣:")
if salary.isdigit(): #判斷輸入是否爲數字,真則執行
salary = int(salary)
else:
print("警告!!!! 請輸入有效金額!。。。")
while True:
print("----------本商城商品--------")
for i,v in enumerate(product_list): #用兩個參數接受商場信息
print(i,"--",v)
user_choice = input("請輸入需要購買的商品【退出:q】:")
if user_choice.isdigit():#判斷用戶選擇是否爲數字
user_choice = int(user_choice)
if user_choice>= 0 and user_choice<len(product_list):#需要大於0或小於商城長度
buy_p = product_list[user_choice]#選擇商品賦值
if buy_p[1] < salary:#判斷選擇商品的價格與用戶金幣關係
shopping_car.append(buy_p)#把購買物品加入購物車
salary = salary - buy_p[1]#剩餘金幣
# print("您已購買%s,"%user_choice[0])
else:
print("金額不足 買不起!!餘額%s"%salary)
else:
print("沒有此商品!")
elif user_choice == 'q':
print("您購買商品如下:")
for i in shopping_car:
print(i)
print('餘額%s'%salary)
break
else:
print("wrong!")
python簡記4--購物車程序
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.