标签:
本文主要是实现:商品的选择及添加到购物车,并与自己的总资产比较,最后结算
代码:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author by lh
shopping=[]
all_price=0
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}
]
for key,i in enumerate(goods):
print key,i
while True:
inp=int(raw_input(‘请输入商品:‘))
shopping.append(goods[inp])
i = int(raw_input(‘请确认是否购买:(输入1结算,其他则继续选择)‘))
if i == 1:
print ‘您购物车中的商品如下:‘
print shopping
for item in shopping:
p=item[‘price‘]
all_price=all_price+p
if shopping[0][‘price‘] > countmoney:
print ‘您的资产不够,请充值!‘
else:
print ‘购买成功!‘
break
else:
print ‘请重新选择商品!‘
运行结果:
标签:
原文地址:http://www.cnblogs.com/pythonlh/p/5730996.html