标签:
本文主要是实现:商品的选择及添加到购物车,并与自己的总资产比较,最后结算
代码:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author by lh
all_price=0
car_dict={}
countmoney=int(raw_input(‘请输入您的总资产:‘))
print ‘您的总资产为:%s元‘% countmoney
goods=[{‘name‘:‘mouse‘,‘price‘:1999},
{‘name‘:‘keyboard‘,‘price‘:200},
{‘name‘:‘computer‘,‘price‘:3000},
{‘name‘:‘mouse pad‘,‘price‘:200}
]
while True:
i2=raw_input(‘请输入您选择的商品:‘)
for item in goods:
if item[‘name‘] == i2:
name = item[‘name‘]
if name in car_dict.keys():
car_dict[name][‘num‘] += 1
else:
car_dict[name] = {‘num‘: 1, ‘price‘: item[‘price‘]}
i=int(raw_input(‘是否确定购买:(结算输入1,其他则继续购买)‘))
if i == 1:
print ‘您购物车中的商品如下:‘
print car_dict
for k,v in car_dict.items():
all_price=v[‘num‘] * v[‘price‘]+all_price
if all_price > countmoney:
print ‘您的余额不足,请充值!‘
else:
print ‘购买成功!‘
break
else:
print ‘请继续选择商品!‘
运行结果:
标签:
原文地址:http://www.cnblogs.com/pythonlh/p/5731202.html