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

Python

Practicing saving a list to a txt file but...

def save(shopping_list):
    # list to a string for open
    shopping_list = " ".join(shopping_list) 
    # open/create w/ "w" permission
    shop_obj = open("shopping_list.txt", "w")
    # pass the string list into the write fn
    shop_obj.write(shopping_list)
    shop_obj = open("shopping_list.txt", "r+")
    shop_obj = shop_obj.read()
    return shop_obj


def retrive(saved_list):
    count = 0
    saved_list = saved_list.split()
    for item in saved_list:
        count += 1
        print("Shopping list {}: ".format(count), item)


shopping_list = []
#call save, store in var
saved_list = save(shopping_list) 
#call retrive
#retrive(saved_list)


while True:
    items = input("Add an item? ").lower().capitalize()
    if items == "S":
        retrive(saved_list) # <-- entering in S does not call  retrive()
    else:
        shopping_list.append(items)
        print(shopping_list)

Can you post the function definition?

did you mean to misspell retrieve?

I thought I looked up retrieve. At least I think I was consistent in my spelling error.

  • I can get my save and retrieve functions to work if I do a static list. When I add things by input it just doesn't seem to be happening.

Try print items after you input statment to make sure .lower().capitalize() is working the way you expect it to.

items = input("Add an item? ").lower().capitalize()
print(items)

the if statment looks correct

Thanks Samuel, I didn't correct it yet but I think you pointed me in the right direction. Now I'm getting indent errors, which it doesn't look like I do but I'm closer to knowing where to look. I think. If you move your comment to an answer I'll give you a thumbs up :D