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

Python; Why does the variable I made NOT recognize updates to my list?

Learning python. I made a list and a variable to recognize updates to said list. Why doesn't the variable I created recognize the .append updates to my grocery list?

Code/image attached http://imgur.com/a/HXEot

You successfully updated shopping list. Now you need to update the variable, thingsweneed, in order for it to reflect those changes made to shopping list. Also, if you check out the Markdown Cheatsheet link below, it will show you how you can include your code in your post vs. attaching a link :). Also, I think renaming your variable to things_we_need would be more Pythonic :)

thanks!!

1 Answer

When you set the variable thingsweneed, you are setting it to hold the list of items at the time the variable is created. It won't update, until you reset the variable. You would need to do that every time you updated the list. Try making it a function, instead, passing the list as an argument.

def thingsweneed(items):
    print("When we go to the store, don't forget these things: ")
    for item in items:
        print(item)

You could format it however you'd like, but that's just an idea.

You could also have a function that adds to the list, and updates that variable at the same time.

I did not get to arguments and creating my own functions yet, but I can sort of see what you're saying. Thank you!

I was wondering that :) Sorry. It will make more sense then, later on. For now, you could just put the thingsweeneed variable at the end of the script. Whenever you are done making the list, basically.

ok great. I guess i was getting ahead of myself! haha. thanks for the help! appreciate it :)