Python学习之路:购物车实例
购物车的流程是真的学了好几遍视频课才把代码跟着老师的讲解抄了一遍啊
product_list=[ ("Iphone",5800), ("Mac Pro",10800), ("Bike",800), ("Watch",10600), ("Coffee",31), ("Alex Python",120), ] # 产品列表 shopping_list=[] # 购物车列表 salary = input("Input your salary:") #输入工资 if salary.isdigit(): # 判断工资是不是数字型 salary = int(salary) # 工资转化成数字型 while True: '''for item in product_list: print(product_list.index(item),item)''' # 取商品编号和商品 方法一 for index,item in enumerate(product_list): # 取商品编号和商品 方法二 print(index,item) 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,you current balance is \033[31;1m%s\033[0m"%(p_item,salary)) # 商品加入购物车,高亮显示余额(33红色,32绿色,[31字体高亮) else: print("\033[41;1m你的余额只剩[%s]啦,还买个毛线啊\033[0m"% salary) # 余额不足提醒 ([41背景高亮) else: print("product code [%s] is not exist!" % user_choice) #商品不存在 elif user_choice == 'q': # 退出打印购物清单 print("------shoping list----") for p in shopping_list: print(p) print("Your current balance:",salary) exit() else: print("Invalid option") # 商品编号无效