python购物车典例_Python入门:购物车实例
product_list=[('iphone',5800),
('pro',120000),
('python book',120),
('Bike',800),
('coffe',39)
] #定義商品列表
shopping_list=[] #定義購物車
salary=input("your sally:") #輸入薪水
if salary.isdigit(): #判斷是否為數(shù)字
salary=int(salary)
while True:
for index,item in enumerate(product_list): #enumerate 取下標(biāo)和內(nèi)容
print(index,item)
user_choice=input("please you choose product:") #輸入商品編號
if user_choice.isdigit():
user_choice=int(user_choice)
if user_choice=0:
p_item=product_list[user_choice]
if salary>=p_item[1]:
shopping_list.append(p_item)
salary=salary-p_item[1]
print("added %s into shopping cart,your current balance is %s"%(p_item,salary))
else:
print("你的余額只剩 %s ,不能買了" %salary)
else:
print("您選擇的不存在")
elif user_choice=="q": #輸入q 退出循環(huán)
print("-------------shopping list----------")
for i in shopping_list: #遍歷打印購物車
print(i)
exit()
else:
print("Invalid option")
else:
print("your salary is wrong")
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的python购物车典例_Python入门:购物车实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 3.9特性_Python
- 下一篇: 战地5 配置要求是什么