购物车程序
Python3 购物车程序
1 #!/usr/bin/evn python 2 # -*- coding:utf-8 -*- 3 # Author:huanu 4 5 ''' 6 程序:购物车程序 7 需求: 8 1、启动程序后,让用户输入工资,然后打印商品列表 9 2、允许用户根据商品编号购买商品 10 3、用户选择商品后,检测余额是否足够,够就直接扣款,不够就提醒 11 4、可随时退出,退出时,打印已购买商品和余额 12 ''' 13 14 product_list = [ 15 ('Iphone',5800), 16 ('Mac Pro',9800), 17 ('Bike', 800), 18 ('Watch', 106000), 19 ('Coffee', 31), 20 ('Alex Python', 120), 21 ] 22 shopping_list=[] 23 salary = input("Input your salary:") 24 if salary.isdigit(): 25 salary = int(salary) 26 while True: 27 for index,item in enumerate(product_list): 28 #print(product_list.index(item),item) 29 print(index,item) 30 user_choice =input("选择要买嘛?>>>:") 31 if user_choice.isdigit(): 32 user_choice = int(user_choice) 33 if user_choice < len(product_list) and user_choice >= 0: 34 p_item = product_list[user_choice] 35 if p_item[1] <= salary: #买得起 36 shopping_list.append(p_item) 37 salary -= p_item[1] 38 39 print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m " %(p_item,salary)) 40 41 else: 42 print("\033[41;1m 你的余额只剩[%s]啦,你还买个毛线啊 \033[0m" % salary) 43 else: 44 print("product code [%s] is not exit!" % user_choice) 45 elif user_choice == 'q': 46 print("------shopping list-------") 47 for p in shopping_list: 48 print(p) 49 print("Your current balance:",salary) 50 exit() 51 else: 52 print("invalid opthion") 53 else: 54 print("您输入的不是数字")
浙公网安备 33010602011771号