From e2c845b60df66cb9520706aeaf163c0fec1a56d1 Mon Sep 17 00:00:00 2001 From: theredwillow Date: Mon, 30 May 2016 19:09:52 -0500 Subject: [PATCH] Initial Contribution Changes: 1. Minor PEP8 fixes 2. Added text file saving and reading functionality for later use and printing needs 4. Added command variable to hold new_stuff().upper() to handle case incompliance in user input 3. Added immediate REMOVE functionality (i.e. "Remove 4" to remove shopping_list[3] without additional dialog, including via strings such as "Remove potatoes" to remove item from shopping_list equal to "potatoes" and error handling for "This isn't in your list.") 4. show_list() now called before asking user which item to remove 5. show_list() now has return_only variable: true to return shopping list, default false to print shopping list - useful for printing inside functions such as input() and write() 6. for loop in show_list() now enumerates to eliminate use of local count variable New requirements: import os.path.isfile --- shopping_list.py | 98 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 70 insertions(+), 28 deletions(-) diff --git a/shopping_list.py b/shopping_list.py index f02f945..5c0c36a 100644 --- a/shopping_list.py +++ b/shopping_list.py @@ -1,54 +1,96 @@ -shopping_list = [] +from os.path import isfile def remove_item(idx): - index = idx -1 - item = shopping_list.pop(index) - print("Remove {}.".format(item)) + try: + index = int(idx) - 1 + item = shopping_list.pop(index) + except: + item = idx + try: + shopping_list.remove(item) + except: + print("This isn't in your list.") + show_list() + return + print("Removed: {}.".format(item)) def show_help(): print("\nSeparate each item with a comma.") print("Type DONE to quit, SHOW to see the current list, REMOVE to delete an d item and HELP to get this message") +def load_list(): + if isfile(file_name): + print("Grocery list found.") + for f in tuple(open(file_name, 'r')): + f = f.split(": ") + if len(f) > 1: + f.pop(0) + f[-1] = f[-1][:-1] + shopping_list.append(": ".join(f)) + return show_list(return_only=True) + +def show_list(return_only=False): + if return_only: + display = "" + else: + display = "\nHere's your list:\n" + + if len(shopping_list) > 0: + for count, item in enumerate(shopping_list): + display += "{}: {}\n".format(count + 1, item) + else: + display = "Your shopping list is empty right now." + + if return_only: + return display + else: + print(display) + +def save_list(): + show_list() + with open(file_name, "w") as text_file: + text_file.write(show_list(return_only=True)) + print("It has been saved.") -def show_list(): - count = 1 - for item in shopping_list: - print ("{}: {}".format(count, item)) - count += 1 +shopping_list = [] +file_name = "grocery_list" + ".txt" -print("Give me a list of things you want to shop for.") +current_list = load_list() +if current_list: + print(current_list, end="") +else: + print("Give me a list of things you want to shop for.") show_help() while True: new_stuff = input("> ") + command = new_stuff.upper() - if new_stuff == "DONE": - print("\nHere's your list:") - show_list() + if command == "DONE": + save_list() break - elif new_stuff == "HELP": + elif command == "HELP": show_help() - continue - elif new_stuff == "SHOW": - show_list() - continue - elif new_stuff == "REMOVE": + elif command == "SHOW": show_list() - idx = input("Which item? Tell me the number.") - remove_item(int(idx)) - continue + elif command.startswith("REMOVE"): + if not new_stuff[7:]: + show_list() + idx = input("Which item? Tell me the number.") + else: + idx = new_stuff[7:] + remove_item(idx) else: new_list = new_stuff.split(",") - index = input("Add this at a certain spot? Press enter for the end of the list, " - "or give me a number. Currently {} item in the list.".format( - len(shopping_list))) + index = "" + if len(shopping_list) > 0: + index = input("Add this at a certain spot? Press enter for the end of the list, " + "or give me a number. This is the current list:\n" + show_list(return_only=True)) if index: - spot = int(index) -1 + spot = int(index) - 1 for item in new_list: shopping_list.insert(spot, item.strip()) spot += 1 else: for item in new_list: shopping_list.append(item.strip()) - -