欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實現(xiàn)購物車小程序

 更新時間:2022年02月08日 15:00:48   作者:%木糖醇---LHY%  
這篇文章主要為大家詳細介紹了python實現(xiàn)購物車小程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

功能實現(xiàn):

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

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

#定義一個列表存放商品信息
products=[('iphone',5800),('bike',220),('vivo',2000),('book',20)]
shopping_list=[]#將購買的商品存在列表shopping_list 中
salary=input("輸入你的工資")
#判斷輸入的工資是否是數(shù)字
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")

效果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論