product_list = [ (‘iphone‘, 5000), (‘mac Pro‘, 12000), (‘bike‘, 800), (‘book‘, 120), (‘watch‘, 10600), (‘coffee‘, 20) ] shopping_list = [] salary = input("input you 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 0 <= user_choice < len(product_list): p_item = product_list[user_choice] if p_item[1] <= salary: shopping_list.append(p_item) salary -= p_item[1] print("%s is added into your shopping_list,the current balance is \033[31;1m%s\033[0m" % (p_item[0], salary)) else: print("\033[41;1m余额不足\033[0m") else: print("\033[41;1m商品不存在,请重新输入\033[0m") elif user_choice == ‘q‘: print("--------shopping_list--------") for p in shopping_list: print(p) print("your current salary:",salary) exit() else: print("\033[41;1m invalid input\033[0m") else: print("\033[41;1m invalid input\033[0m")
原文:https://www.cnblogs.com/TY666/p/10903211.html