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 Python Collections (Retired) Lists Redux Shopping List Take Three

Y. Kravets
Y. Kravets
10,350 Points

Failing to replicate the code from this part

Hi guys! I am trying to simply replicate the code in this video (literally line by line) and while it compiles it seems to refuse to execute the SHOW part of the code. Doesn't show me the list, simply awaits the next input.

Same when I call DONE. It prints: Here is your list with nothing to follow. While I compared my workspace with the code in the video I cannot find a mistake in my code and could really use some help.

The code is as follows:

shopping_list = []

def show_help():
  print("\nSeparate each item with a comma.")
  print("Type DONE to quit, SHOW to see the current list, and HELP to get this message.")

def show_list():
  count = 1
  for item in shopping_list:
    print("{}: {}".format(count, item))
    count += 1

print("Give me a list of things you want to shop for.")
show_help()

while True:
  new_stuff = input(">> ")

  if new_stuff == "DONE":
    print("\nHere is your list:")
    show_list()
    break
  elif new_stuff == "HELP":
    show_help()
    continue    
  elif new_stuff == "SHOW":
    show_list()
    continue
  else:
    new_list = new_stuff.split(",")
    index = input("Add this at a certain spot? Press ENTER for the end of the list, "
                  "or give me a number. Currently {} items in the list.".format(len(shopping_list)))
    if index:
      spot = int(index) - 1
      for item in new_list:
        shopping_list.insert(spot, item.strip())
        spot += 1
      else:
        for item in new_list:
          shopping_list.append(item.strip())

Thanks in advance!

4 Answers

Mikael Enarsson
Mikael Enarsson
7,056 Points

Look at the last else, the indentation is wrong. Semantic errors, am I right?! ><

Y. Kravets
Y. Kravets
10,350 Points

Thanks Mikael! That was indeed a stupid mistake on my part! Will have to be more careful with indentation!

Mikael Enarsson
Mikael Enarsson
7,056 Points

It's not stupid, semantic errors are hard to catch, sometimes even in short pieces of code, since it runs fine, it just doesn't do what you want.

I had to run through your code a couple of times to see what would happen (and write some comments about what every line did in the part of the code I narrowed it down to) to catch this one. Honestly, I personally find it easier when the code uses curly braces.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Good catch on the else:, Mikael Enarsson! We're, hopefully, going to have indentation guides in our Workspaces editor soon (they may already be there!) and that'll help with Python a lot.

Mikael Enarsson
Mikael Enarsson
7,056 Points

That would be wonderful!

William Ruiz
William Ruiz
10,027 Points

I caught myself making this mistake too, today. I look forward to such a feature. Sumblime Text's visual line that drops down for a block of code more helpfully clarifies these indentation errors. Currently I'm relying on the Column number in Workspaces, but the visual block line would be a much better feature.

Raymond Torres
Raymond Torres
2,222 Points
shoppinglist = []

def showhelp():
  print("\nseparate each item with a comma.")
  print("type DONE to quit, SHOW to see the current list, and HELP to get this message.")

def showlist():
  count = 1
  for item in shoppinglist:
    print("{}: {}".format(count, item))
    count +=1

print("give me a list of thigns you want to shop for.")
showhelp()

while True:
  newstuff = input("> ")

  if newstuff == 'DONE':
    print("\n here is your list:")
    showlist()
    break
  elif newstuff == 'HELP':
    showhelp()
    continue
  elif newstuff == 'SHOW':
    showlist()
    continue
  else:
    newlist = newstuff.split(",")
    index = input("add this at a certain spot? press enter for the end of the list,"
    "or give me a number. Currently {} items in thelist.".format(len(shoppinglist)))
    if index:
      spot = int(index) - 1
      for item in newlist:
        shoppinglist.insert(spot, item.strip())
        spot += 1 
    else:
      for item in newlist:
        shoppinglist.append(item.strip())

I can't seem to figure out why mine is not working correctly either. I checked the indents, I did not see anything wrong.