Python初学者第十二天 购物车程序小作业

12day

作业题目: 购物车程序

作业需求:

数据结构:
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
......
]
功能要求:
基础要求:

1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表

2、允许用户根据商品编号购买商品

3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒

4、可随时退出,退出时,打印已购买商品和余额

5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示


扩展需求:

1、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买

2、允许查询之前的消费记录

goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
{"name":"iphone","price":6888},
{"name":"小米6","price":2499}
]
cart = []
salary = int(input("请输入你的工资:"))
while True:
print("商品列表".center(30,'-'))
for index,i in enumerate(goods):
print("%s.%s %s" %(index,i.get('name'),i.get('price')))
choose = input("请输入你要购买的商品编号(按“q”退出,按“c”可查询已购买产品):")
if choose.isdigit():
choose = int(choose)
if choose >= 0 and choose < len(goods):
if salary >= goods[choose].get('price'):
salary -= goods[choose].get('price')
cart.append(goods[choose])
print("你已购买%s" %(goods[choose]),"你的工资还有%s!"%(salary))
else:
print("你的钱不够!")
else:
print("商品不存在")
elif choose == 'c':
if len(cart)>0:
print("你已购买".center(30, '*'))
for index, i in enumerate(cart):
print("%s.%s %s" % (index, i.get('name'), i.get('price')))
print('*' * 34)
print("你的工资余额是%s!" % (salary))
else:
print("你并未购买产品!")
elif choose == 'q':
if len(cart)> 0:
print("购物车".center(30,'-'))
for index, i in enumerate(cart):
print("%s.%s %s" % (index, i.get('name'), i.get('price')))
print('-'*33)
print("你的工资余额是%s!"%(salary))
break
posted @ 2018-01-10 22:05  摩柯无良  阅读(126)  评论(0编辑  收藏  举报