#!/usr/bin/python
import syslog
import sys
this_year=2015
product=['apple','coffee','pc','rebot','clothes','licai'] #创建数组
price=[4999,30,8000,100000,1000,500] #创建数组
qingdan = [] #创建数组
while True:
try:
gongz=int(raw_input('\033[33;1mplease input you gz:\033[0m').strip())
break
#输入工资,不是数值异常处理
#except ValueError:
except ValueError:
print "Error:you must input a num"
while True:
print '\033[35;1mplease looking for caidan\033[0m' #输出你的菜单
for line in product:
#在你的数组中循环
print "\033[32;1m%s\t%s\033[0m" %(line,price[product.index(line)])
#输出你的购物清单
if gongz < min(price): #判断是否购买的起
print "\033[34;1m you didn't buy this is you buy\033[0m"
print "\033[34;1m",qingdan,"\033[0m"
#输出你已经购买的
sys.exit()
else:
goumai = raw_input('\033[32;1m you need shopping,please take it\033[0m')
#选择购买的物品
if goumai=='exit':
#exit 退出判断
print "\033[35;1m you buy ",qingdan,"\033[0m"
sys.exit()
if goumai in product:
if gongz < price[product.index(goumai)]:
#判断是否购买的了
print "\033[31;1myou can't buy",goumai,"youhave",gongz,"\033[0m"
#输出不能买已经清单
continue
else:
qingdan.append(goumai)
#购买的物品添加到数组中
gongz = gongz - price[product.index(goumai)]
#工资减去物品的价格
print "\033[35;1m you buy ",qingdan,"you have ",gongz,"\033[0m"
else:
print "\033[31;1mdon't hava this wp,you have",gongz,"\033[0m"
continue
转载于:https://blog.51cto.com/bluechen/1695956