Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
dh144011
1,231 PointsHow to complete the "Extra Credit" Portion of Shopping List App (Being able to save it as a text file)
So below is my code for the Shopping App. I went above and beyond what was originally asked because I was having fun with it. However I can't seem to complete the extra credit assignment. Here is what my code looks like. I added where I THINK the code should go but I am not really sure of what I am doing
EDIT1: Here is the copy/paste from the course
Extra Credit:
Add a "SAVE" option to your script. Use the open function to open a file and save your shopping list to it.
Then use a for loop on the file to read each line in the file. Add each line to your shopping_list variable when your file first runs.
Your list is now more permanent!
EDIT2: I just discovered that I can use try/except to pretty much take the place of my monster if statements, but thats for another day
def help_command(): #function to print the help menu
print("====CURRENT SUPPORTED COMMANDS===")
print("DELETE: Allows you to delete items from your list")
print("HELP: Displays the help menu")
print("SHOW: Displays current items in your list")
print("DONE: Quits the current mode (Deletion or List Creation)")
def duplicate_input():
print("Ooops! You have already added that item to the list!")
print("Please continue to add items to your list or type DONE")
def blank_input():
print("Ooops! You did not input anything")
print("Please try your input again, or type DONE to continue adding to your list")
def print_list():
for item in shopping_list: #for loop
if item.lower() != "done": #if input is done
print(str(shopping_list.index(item)+1)+":"+' '+item) #prints the list in numerical order
open("test.py" "a")
for "test.py"
shopping_list = [] #make a list to hold onto our items
print("Welcome to my shopping application!") #print instructions on how to use the app
print("To use this app, enter each item you need to remember and hit Enter. When you are finished type DONE!")
help_command()
while True: #ask for new items
new_item = input("> ") #add new items to our list
if new_item.lower() == "show": #SHOW: Show all items on list
print("Here are the items currently on your shopping list!") #prints string
print_list()
elif new_item.lower() == "help": #HELP: Help Message
help_command() #prints help_command
elif new_item.lower() == 'delete': #DELETE: Deletes Inputted Item
while True: #starts a while loop
print("What item would you like to delete? PLEASE NOTE IT IS CASE SENSITIVE!") #prints that string
deletion_item = input("> ") #allows user to set what item to delete
if deletion_item in shopping_list: #checks to see if item is in list
shopping_list.remove(deletion_item) #deletes item from list
print(deletion_item + " has been deleted from your list.") #prints string
print("You may continue entering items for deletion or for type DONE to continue adding items") #prints string
elif deletion_item == "": #if input is empty
blank_input() #return blank_input function
elif deletion_item.lower() == "show": #if input is show
print("Here are the items currently in your list")
print_list() #prints current list
elif deletion_item.lower() != "done" and deletion_item.lower() !="help" and deletion_item not in shopping_list: #checks to see if an input is in list
print("That item is not contained in your shopping list! Try again or type DONE to add more items") #prints string
elif deletion_item.lower() == "help": #displays the help menu
help_command()
elif deletion_item.lower() == "done": #allows the user to finish deleting
print("You are done deleting items, please input more items into your list, or type DONE") #prints string
break #ends while loop
elif new_item in shopping_list: #Checks for duplicate items in list
duplicate_input() #runs duplicate_input function
elif new_item == "": #Checks for a blank input
blank_input() #return blank_input function
elif new_item.lower() == "done": #be able to quit the app
break #breaks while loop
else:
shopping_list.append(new_item) #adds input to list
print("Here is your final shopping list!") #print out the list
print_list() #prints the list in numerical order
print("Thank you for using my application!")
file.close()
1 Answer
Matt Nickele
468 Pointswhen you open a file your reading it, but you can also write to files. You should create an empty file, and then open it in your could. rather than printing to the console you print to this empty file and then at the end close the file. This will save the results