要求:
1.展现商品菜单及其价格
2.输入欲采购的总金额
3.输入商品名称进行采购,如果钱足够购买价格最低商品,则可一直购买,否则提示余额不足,是否要进行充值,如果进行充值则总金额增加,可以继续购物。
4.在开始部分要求对输入进行判断,必须为整形int,否则报错重新输入
5.在选择物品清单的时候,可以输入“?”号进行帮助,帮助内容包括list清单(当前购买的商品都有哪些,且中途充值了一共多少钱,当前余额还剩下多少。)、back(返回上一级菜单)、quit(退出系统)
import sys
item_list = ['aa','bb','cc','dd','ee','ff','gg','hh'] #商品列表
price_list = [5,8,6,3,1,12,24,990] #商品价格
purchase_list = [] #购物车
def latest_list():
for ab in purchase_list:
print ab #打印购物车清单
dict()
amount_money = 0
one = 0
for i in item_list:
print i,price_list[item_list.index(i)] #打印商品及商品价格
while True:
try:
salary = int(raw_input("please input your salary:").strip())
break #确定输入为整形
except ValueError:
print ("please input number,not string!")
continue
while one == 0:
select_item = raw_input("please input your select item name below:").strip()
if select_item == '?':
help_action = raw_input("\033[32mlist Display all of your purchase list.\n"
"back Return to the higher level menu.\n"
"quit Exit this purchase system.\n\033[0m"
"Please input your decide:").strip()
if help_action == 'list':
latest_list()
print 'Your add top-up amount is:',amount_money
print 'Your remaining amount is now:',salary
continue
elif help_action == 'back':
one == 1
elif help_action == 'quit':
sys.exit()
elif select_item == 'quit':
sys.exit()
for a in item_list:
if a == select_item:
purchase_list.append(a) #将买入的商品加入到购物车
salary = salary - price_list[item_list.index(a)] #剩余金额
if salary < min(price_list):
last = raw_input("your money is not enough to buy another something! Will you to top-up (yes/no)?").strip()
if last == 'yes':
top_money = int(raw_input("please input your top-up money:").strip())
amount_money += top_money
salary = salary + top_money #充值金额
one == 1
elif last == 'no':
print ("Thank you ! byebye~~")
sys.exit()
elif salary >= min(price_list):
continue