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 Shopping List Take Three -Help/Explanation needed :/

I just watched the Shopping List Take Three Video, however there are manny things I did not fully understand and also my code does not seem to work correctly as the "SHOW" command does not return anything.

First of all I don't understand the whole placeholder thing, how does it work?

for item in shopping_list:

Secondy I dont get why for, we dont have to use the show_list command again, how does the code now to present me the formated list now?

I know this is really annoying and propably a stupid question but could someone please just explain this to me?

if new_stuff == "DONE":
    print("\nHere's your list:")
    break
shopping_list = []

def show_help():
  print("\nSeperate 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's your 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())

[MODE: fixed formating: extra spaces and missing newline before ```python -cf]

not sure how to get the nice formatting so heres a lini to my workspace: https://w.trhou.se/b52dhdo9p5

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

Hey Jan, you code is very close to correct. Adding items to shopping_list doesn't work if no index is provided. This is caused by an indention error on else:

    if index:
      spot = int(index) - 1
      for item in new_list:
        shopping_list.insert(spot, item.strip())
        spot += 1
    # unindent else
    else:
        for item in new_list:
          shopping_list.append(item.strip())

You are correct in that the show_list call is needed again:

if new_stuff == "DONE":
    print("\nHere's your list:")
    show_list()
    break

The for statement works by iterating (taking one item at a time) on an object, assigning that item to a local variable and supplying that local variable to the code block. The object can be any iterable object such as a list, set, dict, string. For example

for item in "hello":
    print(item)

iterates over the string "hello", setting item to each letter for each execution of the code block print(item). The for command converts the target to an iterable for you, if possible. You can see the list of iterables for an object using list(object).

# running python interactively
>>> list("hello")
['h', 'e', 'l', 'l', 'o']
>>> range(5)
range(0, 5)
>>> for index in range(5):
...     print(index)
... 
0
1
2
3
4
>>> list(range(5))
[0, 1, 2, 3, 4]
>>> list([0, 1, 2, 3, 4])
[0, 1, 2, 3, 4]

# A list is already in an iterable form:
>>> for index in [0, 1, 2, 3, 4]:
...     print(index)
... 
0
1
2
3
4
>>> for index in list([0, 1, 2, 3, 4]):
...     print(index)
... 
0
1
2
3
4
>>>