需求:
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 "请输入正确的商品编号"
二、
salary = input("Input your salary:")
shopping_list = []
shop_list = [("Pen", 100),
("book", 50),
("whitch", 20)]
if salary.isdigit():
salary = int(salary)
while True:
for index, shop in enumerate(shop_list):
print(index, shop)
item_shop = input("Please select the shop that you want to buy:")
if item_shop.isdigit():
item_shop = int(item_shop)
if item_shop < 3 and item_shop >= 0:
if salary > shop_list[item_shop][1]:
shopping_list.append(shop_list[item_shop][0])
salary -= shop_list[item_shop][1]
print("The shop added int the shop car:%s" % shop_list[item_shop][0])
print("Ths remat salay:\033[31;1m%d\033[0m" % (salary))
else:
print("Your remate is not full,please selecct other shop")
continue
else:
print("Your shop code invalid")
elif item_shop == 'q':
print("Shop bought as follw:")
print("-----------------")
for i in shopping_list:
print(i)
print("The remate salary:%s" % (salary))
exit()
else:
print("Your salary have problem ")