产品需求:
1、启动程序后,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
goods_list = [ ('Iphone',5800), ('Mac Pro',9800), ('Bike',800), ('Watch',10600), ('Coffee',31), ('Alex Python',120), ]#商品列表 shooping_cart = []#购物车列表 salary = input("please enter your salary:")#输入工资金额 if salary.isdigit(): salary = int(salary) while True: for index,item in enumerate(goods_list): print(index,item) user_choice = input("需要购买的》》》")#输入商品编号 if user_choice.isdigit():#判断编号是整数 user_choice = int(user_choice) if user_choice <len(goods_list) and user_choice >=0:#判断商品编号存在 p_item = goods_list[user_choice] if p_item[1] <= salary:#可以买的起 shooping_cart.append(p_item) salary -= p_item[1] print("Added the good %s into shopping list,and your current balance is \033[32;1m%s\033[0m" %(p_item,salary)) else:#买不起处理 print("\033[41;1mYour current balance %s is not enough for payed\033[0m" %salary) else:#商品编号不存在 print("product code [%s] is not exist" %user_choice) elif user_choice == 'q':#判断输入的是q,执行退出 print("-----shopping list---------") for i in shooping_cart:#打印购买过的商品列表和余额 print(i) print("Your current balance is %s" %salary) exit() else:#输入不合法字符处理 print("invalid option...")