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 trialNatalie Heyde
1,736 PointsWhy do we need to put the show_list() function at the end?
I was just wondering why we need to put the show_list() function at the end of the file? For some reason my code worked fine without it/looked identical to the example code (maybe out of luck), with the only difference being not putting show_list() at the end. What is the purpose of including it there?
Natalie Heyde
1,736 PointsHi Caleb!
I'm not sure what you mean? I think I might've been confusing with my wording. I defined the function show_list() in the file and the code worked just fine. I was just wondering why in the video, Craig put the show_list() function again at the end, after the add_to_list(new_item)? I apologize if I'm misinterpreting; here's what I did:
shopping_list = []
def show_help():
print("What should we pick up at the store?")
print("""
Enter 'DONE' to stop adding items.
Enter 'HELP' for this help.
Enter 'SHOW' to display the list.
""")
def add_to_list(item):
shopping_list.append(item)
print("Your item was added to the shopping list. The list currently has {} item(s).".format(len(shopping_list)))
def show_list():
print("Here's your list:")
for item in shopping_list:
print(item)
show_help()
while True:
new_item = input("> ")
if new_item == 'DONE':
break
elif new_item == 'HELP':
show_help()
continue
elif new_item == 'SHOW':
show_list()
continue
add_to_list(new_item)
Natalie Heyde
1,736 PointsOh, that makes total sense! Thank you!
Caleb Kemp
12,755 PointsHappy I was able to help
2 Answers
Caleb Kemp
12,755 PointsYour right, sorry I did misunderstand. So, I think the question is, why does Craig have a call to show_list()
at the end since it doesn't seem to do anything? right?
Well, with the show_list()
at the end, after you have finished making your list and type in "DONE", it will print out a list of the items you added to the cart like so
Here's your list:
eggs
cheese
milk
etc.
Without it (show_list()
), it will not print the list once it finishes. I hope that helps
Michael Jacoby
Front End Web Development Techdegree Student 4,708 PointsOne thing I wanted to mention is what Craig mentions quite often, and that's that we're not going to break anything by changing the code. So this is a good example.
Test 1. # show_code() With it commented out, run the code, and see what happens.
Test 2: show_code() Uncomment it, then see.
I was a bit confused about various aspects of the code myself, so I broke the code, changed it, commented it out, and that helped me see the end result.
In this case - as the comments indicate - the shopping list displays when DONE.
Caleb Kemp
12,755 PointsCaleb Kemp
12,755 PointsOne thing to remember is that in Python, white-space characters (tabs, newlines, etc), can actually change how your program runs. Depending on how you moved it, accidentally pasting in extra white-space character, pasting in a way that the program thinks your code is now in a method (def), are all things that could have made it fail. Another thing to consider is if the code depends on some other code having run, changing the order can make it fail. I tried moving the call away from the bottom and received no error. Hopefully that helps!