#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Deakin"
# Email: 469792427@qq.com
# Date: 2018/1/2
#程序:购物车程序
#需求:
#启动程序后,让用户输入工资,然后打印商品列表
#允许用户根据商品编号购买商品
#用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
#可随时退出,退出时,打印已购买商品和余额
product_list=[
("iphone",8000),
("computer",6000),
("car",30000),
("coffee",30),
("watch",500),
("book",80),
]
shopping_list=[]
salary=input("please key in your salary:")
if salary.isdigit():
salary=int(salary)
while True:
#for item in product_list:
#print(product_list.index(item),item)
#break
for index,item in enumerate(product_list):
print(index,item)
print("选择要购买的商品编号,按q退出")
user_choice=input("选择要购买的商品:")
if user_choice.isdigit():
user_choice=int(user_choice)
if user_choice<len(product_list) and user_choice>=0:
p_item=product_list[user_choice]
if p_item[1]<=salary:#买得起
shopping_list.append(p_item)
salary-=p_item[1]
print("added %s into shopping cart,your current balance is \033[31;1m%s\033[0m"%(p_item,salary))
else:#买不起
print("\033[41;1m穷逼,你的余额只有%s了,还不赶紧充钱\033[0m"%(salary))
else:
print("the product is not exist")
elif user_choice==‘q‘:
print("-------shopping list-------")
for p in shopping_list:
print(p)
print("your balance is :",salary)
exit()
else:
print("invalid opation")