__author__ = 'xitaozi' ''' 使用列表实现购物车 ''' produce_list = [ ('Macbook pro 15.4',14288), ('Think pad X1 carbon',10000), ('HP WASD',7000), ('Dell xps 13',8000) ] while True: budget = input("请输入购物预算:") shopping_list = [] if budget.isdigit(): budget = int(budget) while True: for (item,product) in enumerate(produce_list): print(item,product) user_choice = input("选择商品编号,加入购物车。输入字母'Q'退出") if user_choice.isdigit(): user_choice = int(user_choice) if user_choice < len(produce_list) and user_choice >= 0: p_item = produce_list[user_choice] if p_item[1] <= budget: shopping_list.append(p_item) budget = budget - p_item[1] print("加入%s购物车成功,您的预算余额为:%d。"%(p_item,budget)) else: print("您的预算余额为:%d,无法继续购买。"%budget) else: print("商品编号输入错误,请重新输入。") elif user_choice == 'Q'or user_choice == 'q': print("--------您的购物车有如下商品--------") for shop_item in shopping_list: print(shop_item) exit() else: print("输入错误") else: print("输入错误,请重新运行程序。")