日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

python實現購物車小程序_python

作者:%木糖醇---LHY% ? 更新時間: 2022-04-14 編程語言

本文實例為大家分享了python實現購物車小程序的具體代碼,供大家參考,具體內容如下

功能實現:

(1)可以查看購物車的商品,和余額

(2)可以顯示商品列表,根據商品的編號選擇商品

#定義一個列表存放商品信息
products=[('iphone',5800),('bike',220),('vivo',2000),('book',20)]
shopping_list=[]#將購買的商品存在列表shopping_list 中
salary=input("輸入你的工資")
#判斷輸入的工資是否是數字
if salary.isdigit():
? ? salary=int(salary)#把輸入的工資轉換為整型
? ? while True:
? ? ? ? # for i in products:輸出商品編號和商品信息兩種方式均可
? ? ? ? # ? ? print(products.index(i),i)
? ? ? ? for index,i in enumerate(products):
? ? ? ? ? ? print(index,i)
? ? ? ? user_choice=input("選擇要買的商品")
? ? ? ? #通過輸入商品編號來選擇商品
? ? ? ? if user_choice.isdigit():
? ? ? ? ? ? user_choice=int(user_choice)
? ? ? ? ? ? if user_choice>=0 and user_choice<len(products):
? ? ? ? ? ? ? ? p_item=products[user_choice]
? ? ? ? ? ? ? ? if p_item[1]<=salary:
? ? ? ? ? ? ? ? ? ? shopping_list.append(p_item)#把買的東西放到購物車
? ? ? ? ? ? ? ? ? ? salary-=p_item[1]#計算買完東西以后剩下的錢
? ? ? ? ? ? ? ? ? ? print("add %s into shopping car,you current salary %s" %(p_item[0],salary))
? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? print("\033[41;1m你的余額只剩[%s]了,不能買了,按q退出\033[0m" %salary)
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? print("商品不存在")
? ? ? ? elif user_choice=='q':
? ? ? ? ? ? print("購物車的商品如下所示")
? ? ? ? ? ? for p in shopping_list:
? ? ? ? ? ? ? ? print(p)
? ? ? ? ? ? print("余額",salary)
? ? ? ? ? ? break
? ? ? ? else:
? ? ? ? ? ? print("invalid input")
else:
? ? print("invalid input")

效果:

原文鏈接:https://www.cnblogs.com/come-on-baby/p/9480552.html

欄目分類
最近更新