Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.