**
python购物小程序
**
#-- coding:utf-8 --
“”“要求
程序运行时输入自己的工资slary
输入工资之后展示商品列表
用户输入编号表示购买商品
用户输入q表示退出,并展示用户一共购买的商品和花的钱
要满足所有的异常处理,比如输入的工资不能为空
用户工资不够买商品时提示用户slary不够”""
shops=[[0,‘iphone’,5800],[1,“max pro”,12000],[2,‘bike’,800]]
shoplist=[]
try:
slary=int(input(‘slary:’))
except ValueError:
print(‘请输入正确的工资’)
exit(1)
for s,i,k in shops:
print(s,i,k)
while True:
shopsnum=input(‘请输入您要购买的商品编号或者按q退出:’)
if shopsnum in [str(s) for s,i,k in shops]:
shopsnum = int(shopsnum)
if slary>=shops[shopsnum][2]:
yu_e=slary-shops[shopsnum][2]
print(‘您已成功购买%s,slary还剩%d’ % (shops[shopsnum][1],yu_e))
shoplist.append(shops[shopsnum])
else:
print(‘不好意思,您的slary不足已购买这件商品,请重新选择,或者退出’)
elif shopsnum==‘q’:
cost=slary-yu_e
leg=len(shoplist)
print(‘您本次购买了%s,一共花费了%d’ % (tuple([s for i,s,k in shoplist]),cost))
print(‘bye-bye’)
exit(1)
else:
print(‘您输入的商品编码有误,请重新输入’)