小程序-shopping car

购物车程序示例
本文介绍了一个简单的Python购物车程序实现。用户可以根据自己的预算选购商品,程序会实时更新用户的剩余金额,并记录已选购的商品。文章提供了完整的代码示例。
product_list = [("mac",9000),
("bike",1500),
("book",80),
("tesla",900000),
("kindle",800)] #此处需注意:元组的书写方式,如果写成了"mac,9000",则9000的格式为string且无法比较
salary = input("please input your salary:")
shopping_car = []
if salary.isdigit():
salary = int(salary)
while True:
for i,v in enumerate(product_list,1):#enumerate每次回返回一个tuple:(index, value)
print(i, ">>>",v)
# while True: # 如果条件判断语句永远为 true,循环将会无限的执行下去,此处表示可以循环选择
choice =input("please enter your choice[q:quit]:")#如果将choice 缩进了,一次只得到一个输出1 >>> mac,9000
# 2>>>bike,1500 3>>>book,80,因为 enumerate 函数表示遍历该列表,如果缩进相当于 i=1,选择一次;i=2,选择一次
#i =3,又选择一次,循环的层次不对
if choice.isdigit():
choice = int(choice)
if choice >0 and choice<= len(product_list) :#为什么用for choice >0 and choice<= len(product_list)报错,can't assign to operator
p_item = product_list[choice-1]
if p_item[1] < salary:
salary -= p_item[1]
shopping_car.append(p_item)
else:
print("余额不足")
else:
print("选择不在商品列表中")
elif choice == "q":
print("退出,您已购买如下商品")
for a in shopping_car:#遍历
print(a)

# print(shopping_car)
print("余额%s"%salary)
break
else:
print("请输入数字")
else:
print("please input digits.")

转载于:https://www.cnblogs.com/ljk21313/p/6958953.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值