''' 作者:T.song 需求: 1. 启动程序后,让用户输入工资,然后打印商品列表 2. 允许用户根据商品编号购买商品 3. 用户选择商品后,检测余额是否足够,够就直接扣款,不够就提醒 4. 可随时退出,退出时,打印已经购买的商品和余额 ''' shopping_list = [] product_list = [ ('ipone',5800), ('Mac Pro',9800), ('Bike',800), ('Watch',10600), ('Coffee',31), ('Alex python',120) ] salary = input("请输入您的工资:") if salary.isdigit():# 是不是一个数字 salary = int(salary) while True: # 输出所有商品列表 for index,item in enumerate(product_list): print(index,item) user_choice = input("选择要buy什么?>>>:") if user_choice.isdigit(): user_choice_num = int(user_choice) # 输入数字小于商品编号且输入数字大于等于0 if user_choice_num < len(product_list) and user_choice_num >= 0: # 获取商品价格 p_item = product_list[user_choice_num] if p_item[1] <= salary: #代表能两级数组,括号里面的第一个参数 shopping_list.append(p_item) salary -= p_item[1] print("Added %s into shopping cart,your current balance is %s" %(p_item,salary)) else: print("your banalce have [%s]"%(salary)) else: print("商品不存在····") elif user_choice == "q": print("---------------shopping list-------------") for p in shopping_list: print(p) print("your current balance:",salary) exit() else: print("invalid option····")
python购物车小程序(教学版)
最新推荐文章于 2022-01-14 11:27:50 发布