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 Display the List

Ceil-Ian Maralit
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Ceil-Ian Maralit
Front End Web Development Techdegree Graduate 19,434 Points

Quick question

Hi! This is my code: You'll see what my question is in the code :)

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 show all the items on your list
""")


def add_to_list(item):
    shopping_list.append(item)
    print("{} is added to your list. You now have a total number of {} item(s) on your list.".format(new_item.title(), len(shopping_list)))


def show_list():
    print("The item(s) on your list are/is:")
    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() #This already does the right thing
        continue

    add_to_list(new_item)
    print(shopping_list)

show_list() # I don't understand why we need to call this again here

I thought maybe it's for code efficiency. Is it? Thanks!

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

If you look at the entire process it is basically:

  1. Show the help text to let the user know their options
  2. Process with a loop allowing the user to add items, quit, show the help text or show the list
  3. After the process loop show the list

So, it is called again at the end to finish by showing the finalized loop. Not really code efficiency but user friendliness to show the final list.

2 Answers

Chandelor Simon
Chandelor Simon
2,242 Points

Ciel-Ian: I don't understand why we need to call this again here

Chandelor: You don't actually HAVE to call it again to make the program work. This is done for the sake of being user friendly. One of the steps we wanted to take when writing this code was: make it so that the user can print at the end.

In theory: the user could just 'SHOW' the list at the end when they're finished before saying he/she is 'DONE'. Thing is: if you take out that last line of the code (show_list()) and then run it, say you're 'DONE' and then you want your list to 'SHOW' boop - the program is already over and you can't do that; nice try.

So we avoid that problem by having the list show on it's own when the user says he/she is 'DONE'.