day_1 练习2

#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
b. 功能要求:
v = 2000
要求用户输入总资产,例如:2000
显示商品列表,让用户根据序号选择商品,加入购物车
购买,如果商品总额大于总资产,提示账户余额不足,
否则,购买成功。
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
num = input('>>>') # 1
num = int(num)
goods[num]['price']
'''
v=2000
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
n=0
GMSP=[]
for i in goods:
print (n,i)
n+=1
while True:
num = int(input("请输入您要购买商品所对应的序号:").strip())
if v > goods[num]['price']:
v -= goods[num]['price']
print ("您消费了:",goods[num]['price'])
GMSP.append(goods[num]['name'])
print ("您已成功购买",goods[num]['name'])
print ("您的余额是",v)
else:
print ("余额不足")
break
print ("您今天所购买的商品有:",GMSP)
print ('欢迎下次光临')
posted @ 2017-04-24 17:06  昂达达  阅读(208)  评论(0编辑  收藏  举报