需求:
1、启动程序后,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
# -*- coding:utf-8 -*-
salary=int(raw_input("请输入工资:"))
acquire=[]
shop=["pen","book","bootle","umbrella"]
while 1:
line=[10,20,30,40]
show=‘‘‘1、pen:%d\n2、book:%d\n3、boottle:%d\n4、umbrella:%d‘‘‘%(line[0],line[1],line[2],line[3])
print show
select=raw_input("请选择需要购买的商品,若没有想买的商品请输入q:")
if select==‘q‘:
print "已购买商品:",acquire
print "您的余额:",salary
break
elif int(select)==1:
if salary-line[0]<0:
print "您的余额不足请选择其它商品"
continue
else:
acquire.append(shop[0])
salary = salary - line[0]
elif int(select)<5:
i=int(select)
if salary-line[i-1]<0:
print "您的余额不足请选择其它商品"
continue
else:
acquire.append(shop[i-1])
salary=salary-line[i-1]
else:
print "请输入正确的商品编号"
原文:http://www.cnblogs.com/date20170814/p/7384820.html