程序讲解: 商品列表是动态的,可能随时增加,减少,所以不能写死 Step1:打印商品列表 方式1 product_list = [ ('Iphone',5800), ('Mac Pro',9800), ('Bike',800), ('Watch',10600), ('Coffee',31), ('Alex Python',120), ] 工资只输入一次,所以放循环外面 salary = input("Input your salary: ") if salary.isdigit(): salary = int(salary) while True: for item in product_list: print(product_list.index(item), item) break 方式2 不通过下标,一个更直接的方法 product_list = [ ('Iphone',5800), ('Mac Pro',9800), ('Bike',800), ('Watch',10600), ('Coffee',31), ('Alex Python',120), ] salary = input("Input your salary: ") if salary.isdigit(): salary = int(salary) while True: for index, item in enumerate(product_list): print(index, item) break 1. 关于 enumerate >>> a = [1,2,3] >>> for i in enumerate(a):print(i) ... (0, 1) (1, 2) (2, 3) 每一个 i 都变成了 一个元组 第一个是小标,第二个是数据本身 enumerate 做的就是 取出了 列表的下标 Step2:根据用户选择做判断 product_list = [ ('Iphone',5800), ('Mac Pro',9800), ('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 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, your current balance is %s" %(p_item, salary)) elif user_choice == 'q': print('exit....') else: print("invalid option") 1. 如果想给结果变个颜色,红色 31,绿色 32 print("Added %s into shopping cart, your current balance is 33[31;1m%s 33[0m" %(p_item, salary)) 这个就是死记,后面 033[0m 一定要关闭 product_list = [ ('Iphone',5800), ('Mac Pro',9800), ('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 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, your current balance is 33[31;1m%s 33[0m" %(p_item, salary)) elif user_choice == 'q': print('exit....') else: print("invalid option") 2. 这是能买得起的情况,如果买不起呢? product_list = [ ('Iphone',5800), ('Mac Pro',9800), ('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 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, your current balance is 33[31;1m%s 33[0m" %(p_item, salary)) else: print("