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

Why is my Try/Except block in my add_to_list function still giving me a ValueError?

import os
shopping_list = []

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

def num_items(shopping_list):
    print ("List has {} items.".format(len(shopping_list)))

def add_to_list(item):
    if len(shopping_list):
        position = int(input("Where are we putting {}?\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)
    show_list(shopping_list)



def remove_item(item):
    item = input("Which item would you like to remove?  ")
    item = item.upper()
    shopping_list.remove(item)

def show_help():
    clear_screen()
    print("What should we get at the store?")
    print("""
Enter 'DONE' to stop adding items.
Enter 'HELP' to show this menu.
Enter 'SHOW' to show list.
Enter 'REMOVE' to remove items
Enter 'CLEAR' to clear the list
""") 
    show_list(shopping_list)


def show_list(shopping_list):
    clear_screen()
    print ("List has {} items.".format(len(shopping_list))) 
    index = 1
    for item in shopping_list:
        print ("{}. ".format(index) +item)
        index += 1        
    print ("-"*10)    

def clear_list(shopping_list):
    while len(shopping_list) > 0:
            for item in shopping_list:
                shopping_list.remove(item)
                show_list(shopping_list)

#THIS IS THE LOGIC 
show_help()
while True:
    new_item = input("> ")
    if new_item.upper() == 'DONE' or new_item.upper() == 'QUIT':
        show_list(shopping_list)
        break
    elif new_item.upper() == 'HELP':
        show_help()
        continue
    elif new_item.upper() == 'SHOW':
        show_list(shopping_list)
        continue
    elif new_item.upper() == 'REMOVE': 
        remove_item(new_item)
        show_list(shopping_list)
    elif new_item.upper() == 'CLEAR':
        clear_list(shopping_list)
        continue        
    else:
        new_item = new_item.upper()
        add_to_list(new_item)

I am trying to have it add the item at the bottom of the list if I press Enter (' '). This is the Traceback I get with an example of how it should run.

List has 2 items.

  1. PEAS

2. CORN

eggs
Where are we putting EGGS?

Traceback (most recent call last):
File "shopping_list.py", line 83, in <module>
add_to_list(new_item)
File "shopping_list.py", line 12, in add_to_list
position = int(input("Where are we putting {}?\n ".format(item)))
ValueError: invalid literal for int() with base 10: ''

1 Answer

This line is where your error is because you are trying to convert an empty string to integer

 position = int(input("Where are we putting {}?\n  ".format(item)))    

This is not handled because it is outside your try block. You could move the code as follows:

def add_to_list(item):

    try:
        if len(shopping_list):
            position = int(input("Where are we putting {}?\n  ".format(item)))    
        else:
            position = 0

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

Ahhhhh it all makes sense now! Obviously you can't turn nothing into an integer, I just wish I had thought of that a few hours ago! Don't you love the frustrations of learning.