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 (2016, retired 2019) Lists Removing Items From A List

Not sure where I have gone wrong with my code

Not sure where I have gone wrong. I have looked at the error messages and tried to see if there is anything I can visibly notice, to no avail. Please help me, it's driving me up the wall

Full code:

```import os

shopping_list = []

def clear_screen(): os.system("cs" if os.name == "nt" else "clear")

def show_help(): clear_screen() 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 list. Enter 'REMOVE' to remove an item for your list. """)

def add_to_list(item): show_list() if len(shopping_list): position = input("Where should I add {}?\n" "Press ENTER to add to the end of the list\n" "> ",format(item)) else: position = 0

try:
    position = abs(int(position))
except ValueError:
    position = None
if position is not None:
    shopping_list.insert(position-1, item)
else:
    shopping_list.append(new_item)
shopping_list.append(new_item)

show_list()


print("Added! List has {} items.".format(len(shopping_list)))

def show_list(): clear_screen()

print("Here's your list:")

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

print("-" + 10)

def remove_from_list(): show_list() what_to_remove = input("What would you like to remove?\n> ") try: shopping_list.remove(what_to_remove) except ValueError: pass show_list()

show_help()

while True: new_item = input("> ")

if new_item.upper() == 'DONE' or new_item.upper() == 'QUIT':
    break
elif new_item.upper() == 'HELP':
    show_help()
    continue
elif new_item.upper() == 'SHOW':
    show_list()
    continue
elif new_item.upper() == 'REMOVE':
    remove_from_list()
    continue
else:
    add_to_list(new_item)

show_list()

3 Answers

A couple things

In your add_to_list(item) function you have the following statements listed twice

else:
    shopping_list.append(new_item)
    shopping_list.append(new_item)

You also need to use a period instead of a comma before format here:

position = input("Where should I add {}?\n"
                         "Press ENTER to add to the end of the list\n"
                         "> ",format(item))

should be

position = input("Where should I add {}?\n"
                         "Press ENTER to add to the end of the list\n"
                         "> ".format(item))

In your show_list() function

print("-" + 10)

should be

 print("-"*10)

Thank you! I did not notice these errors at all lol it's not working

Luca James
Luca James
4,433 Points
def clear_screen(): os.system("cs" if os.name == "nt" else "clear")

"cs" should be "cls" right off the bat too

boi
boi
14,242 Points

I fixed all the errors that were showing, now your code is running fine. Just compare this code with yours, and you will figure out the errors. :) BOI

import os

shopping_list = []

def clear_screen(): 
  os.system("cls" if os.name == "nt" else "clear")

def show_help(): 
  clear_screen() 
  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 list. Enter
        'REMOVE' to remove an item for your list. """)

def add_to_list(new_item): 
  show_list() 
  if len(shopping_list):
    position = input("Where should I add {}?\n" "Press ENTER to add to the end of the list\n" "> ".format(new_item)) 
  else: 
    position = 0

  try:

   position = abs(int(position))
  except ValueError:

    position = None
  if position is not None:

    shopping_list.insert(position-1, new_item)
  else:

    shopping_list.append(new_item)


  show_list()


print("Added! List has {} items.".format(len(shopping_list)))
def show_list(): 
  clear_screen()

  print("Here's your list:")

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

  print("-" * 10)
def remove_from_list(): 
  show_list() 
  what_to_remove = input("What would you like to remove?\n> ") 
  try: 
    shopping_list.remove(what_to_remove) 
  except ValueError: 
    pass 
  show_list()

show_help()

while True: 
  new_item = input("> ")

  if new_item.upper() == 'DONE' or new_item.upper() == 'QUIT':
    break

  elif new_item.upper() == 'HELP':
      show_help()
      continue
  elif new_item.upper() == 'SHOW':
      show_list()
      continue
  elif new_item.upper() == 'REMOVE':
      remove_from_list()
      continue
  else:
      add_to_list(new_item)
  show_list()