Python Introducing Lists Build an Application Add Items

J R
PRO
J R
Pro Student 12,023 Points

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.