product_list = [
('Iphone',8000),
('Mac Pro',9800),
('Bike',800),
('Watch',10600),
('Coffee',30),
]
shopping_list = []
salary = input("Input your salary:")
if salary.isdigit():
salary = int(salary)
while True:
for index,item in enumerate(product_list):
print(index,item)
user_choice = input("选择要买吗?>>>:")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice >=0:
p_item = product_list[user_choice]
if p_item[1] <=salary:
shopping_list.append(p_item)
salary -= p_item[1]
print("Your current balance:",salary)
else:
print("余额不足")
else:
print("product code [%s] is not exist!"% user_choice)
elif user_choice =='q':
print("-----shopping list------")
print(shopping_list)
for p in shopping_list:
print(p)
print("Your current balance:",salary)
exit()
else:
print("invalid option")
Python购物车程序初练
原创
©著作权归作者所有:来自51CTO博客作者龙zero的原创作品,请联系作者获取转载授权,否则将追究法律责任

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
购物车的实现(未登录时也可以使用)
平时工作的踩坑记录
redis List 拦截器 -
python之购物车程序
题目:购物车程序业需求
python 小练 -
Python开发购物车程序
用Python开发一个购物车程序案例
脚本 Python 购物车 -
Python简单的购物车程序
刚开始学习Python做的购物车练习
Python 购物车