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 Introducing Lists Build an Application Add Items

Add_to_list Function

In the example for this video, they call the 'add_to_list' function for the parameter 'new_item' in the last line. I did not indent this line of code on my editor and so when I ran the program in the shell, the prompt "There are {} items in the list" did not appear until I finished the program, and it gave me a total count of items.

When I indented this line it then behaved as in the video. Could I get an answer for why this is?

1 Answer

The add_to_list, when indented, will run the function every time a new item is added until 'DONE' is entered (the loop will break and exit) so that statement will be printed everytime the user inputs something.
When the add_to_list is not indented, it will not run at all until breaking out of the while loop. That is why you didn't see the print statement with the count of items until the end of the program because the function that prints to the console was not executing until the while loop was broken out of. I hope this helps.