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
Freddy Venegas
1,226 PointsPython - Shopping List extra credit problem
While attempting to scale the shopping list script with the extra credits steps:
- Add a "SAVE" option to your script. (Done)
- Use the open function to open a file and save your shopping list to it. (Done)
- 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. I seem to be stuck on the last two steps, using a for loop to reach each line in the file and adding each line to my shopping_list variable.
The closest I got ended up adding my entries into my external file infinitely.
If someone could point me in the right direction, that'd be much appreciated.
This is my current open and save function:
def open_save():
with open('list.txt', 'w') as l:
for items in shopping_list:
l.write(str(items) + "\n")
and I am calling it in my main function like this:
...
elif new_item == 'SAVE':
open_save()
continue
5 Answers
Freddy Venegas
1,226 PointsIn case someone stumbles on this, I ended up figuring out how to add the lines from my external file to my shopping_list variable. I did it like so (there's probably another, more elegant, way):
with open('list.txt', 'r') as f:
shopping_list = f.read().splitlines()
I added the lines above before all my functions, so, when the script first runs it reads this first. This, combined with other functions, makes it so the list never has dupes.
Also, I did try using,
f.readlines()
but this was also adding the \n to my shopping_list variable, which I did not want.
Freddy Venegas
1,226 Pointsxela888 the first character is an 'l' not a number, I think you're referring to [it] looking like a 1.
What I am looking to do is add the items from my .txt file to my shopping_list variable
- Add each line to your shopping_list variable when your file first runs.
Alexander Davison
65,469 PointsOh sorry XD
Alexander Davison
65,469 PointsTry using the readline function. You can review the videos to learn about it
julio cesar
2,731 Pointsdef open_save():
print("enter the name of your file")
file = input('Enter your name: ')
try:
with open(file, 'w') as file:
for items in shopping_list:
file.write(str(items) + "\n")
except:
print("try again")
try my code it completely works, just add the .txt when u name your file like "name.txt", and thanks btw it really helped me.
Frank Campos
4,175 Pointsdef addtolist(item):
add= list.append(newitem)
print("added {} now I have {}items".format(newitem,len(lista)))
file=open('createalisst.txt','a')
file.write(item+"\n")
this worked for me, I did not create a new loop. I just took the newitem(input) for the while loop
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsSo in Python you cannot use numbers as the first letter of a name of a variable, function, etc. If you did, Python will think you are using a number. Try this out for your open_save function: