Python 购物车实例
一、作业需求:
1、打印购物列表
2、购买物品,添加到购物车
3、显示余额
4、退出时打印购买列表
二、Python实例
1 product_list = [ 2 ('Iphone',5800), 3 ('Mac Pro',12000), 4 ('Bike',800), 5 ('Coffice',32), 6 ('Book',80) 7 ] 8 shopping_list=[] 9 salary = input("请输入您的工资:") 10 if salary.isdigit(): 11 salary = int(salary) 12 while True: 13 for index,item in enumerate(product_list): 14 print(index,item) 15 user_choice = input("请输入你要买的物品>>>") 16 if user_choice.isdigit(): 17 user_choice = int(user_choice) 18 if user_choice < len(product_list) and user_choice >=0: 19 buy_item = product_list[user_choice] 20 if buy_item[1] < salary: 21 shopping_list.append(buy_item) 22 salary -= buy_item[1] 23 print("您买的[%s]已经添加到购物车,您的余额还剩余\033[31;1m%s\033[0m。" %(buy_item,salary)) 24 else: 25 print("\033[41;1m您的余额为%s,什么都不能买了\033[0m。" %salary) 26 else: 27 print("您输入的参数有误") 28 elif user_choice == 'q': 29 print("------- 购物车 --------") 30 for buy in shopping_list: 31 print(buy) 32 print("您的余额还剩余",salary) 33 exit() 34 else: 35 print("Invalid option")

浙公网安备 33010602011771号