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

Kristina Bautista
Kristina Bautista
2,611 Points

My append function doesn't seem to work for the shopping list application. When I click "show" my list is empty.

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 see your current lsit.
    """)


def add_to_list(item):
    shopping_list.append(item)
    print("Added! List has {} items.".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)

show_list()

3 Answers

Steven Parker
Steven Parker
229,644 Points

Without formatting, there's no obvious error in the code. But indentation is critical to Python, so if something is not properly indented that could cause a problem.

When posting code, always use "Markdown" formatting to preserve indentation. Instructions can be found in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:

Or for more detailed instructions, you can watch this video on code formatting.

Kristina Bautista
Kristina Bautista
2,611 Points

Whoops, sorry about that! Thanks for the info, will do!

Steven Parker
Steven Parker
229,644 Points

Now that it's formatted, it looks like the indentation amount switches from 2 spaces per level to 4 spaces per level at different points in the file. It's important that the indentation amount per level remain consistent. Pick the amount you prefer (either will work) and fix it so all lines use the same amount per level.

It also looks like line 35 ("add_to_list(new_item)") is indented too much. It should be part of the loop, but not part of the "elif".

Kristina Bautista
Kristina Bautista
2,611 Points

Ohh, I see! Thanks so much! That's very helpful!!

Kristina Bautista
Kristina Bautista
2,611 Points

Thanks again Steven Parker ! For some reason I wasn't able to select your second detailed answer as best answer =/ (perhaps because it was as a reply?) If you want to repost it as a new answer I will mark that one! :) in the meantime, I selected your first one!

Steven Parker
Steven Parker
229,644 Points

My second post is not a separate answer, but a comment to the first answer. So it's essentially covered by the same choice.

Thanks, and happy coding!