程序:购物车程序
需求:
- 启动程序后,让用户输入工资,然后打印商品列表
- 允许用户根据商品编号购买商品
- 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
- 可随时退出,退出时,打印已购买商品和余额
购物车作业代码
1 #Author:abu zhang 2 3 import sys 4 shop_list = [ 5 ("IphoneX",9000), 6 ("mac book",19000), 7 ("IphoneX",9000), 8 ("Netbook computer",5000), 9 ("linux python book",90), 10 ("coffee",54) 11 ] 12 shopping_cart = [] 13 try: 14 salary = input("Please enter your salary:") 15 if salary.isdigit(): 16 salary = int(salary) 17 while True: 18 for k, v in enumerate(shop_list): 19 print(k, v) 20 user_input = input("Please choose the item you want to buy ,input 'quit' exit:") 21 22 if user_input.isdigit(): 23 user_input = int(user_input) 24 if user_input < len(shop_list) and user_input >= 0: 25 p_item = shop_list[user_input] 26 if salary > p_item[1]: 27 shopping_cart.append(p_item) 28 salary = salary - p_item[1] 29 print("you have purchased \033[31;1m%s\033[0m; shopping,amount remaining \033[31;1m%s\033[0m"%(p_item[0],salary)) 30 else: 31 print("您的余额不足矣购买该商品。") 32 else: 33 print("\033[31;1m找不到您想要购买的商品,请重新输入\033[0m") 34 elif user_input == "quit": 35 print("欢迎下次光临!") 36 print("您已购买了如下商品:") 37 print("\t\t------shop list-----") 38 for i in shopping_cart: 39 print("\t\t",i) 40 break 41 finally: 42 print("Input Error")