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!

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

Chang Hyeon Lee
Chang Hyeon Lee
2,008 Points

I need some help with UnboundlocalError

now I am doing shopping list three and I followed the lecture like this:

import os

# make a list to hold onto our items
shopping_list = []

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

def show_help():
    clear_screen()
    # print out instructions on how to use the app
    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.
""")  



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"
                         "> ".fomat(item))

    else:
        postion = 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)

    shoe_list()

    # add new items to our list
    shopping_list.append(new_item)
    print("Added {}. List now has {} items.".format(new_item, len(shopping_list)))



def show_list():
    clear_screen()
    # print out the list

    print("Here's your list:")

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

    print("-"*10)

show_help()    

while True: 
    # ask for new items
    new_item = input("> ")

    # be able to quit the app
    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
    else:
        add_to_list(new_item)      

show_list()

when I import this file it was working but when I entered the item, I got this message

Traceback (most recent call last):                                                                              
  File "shopping_list_3.py", line 77, in <module>                                                               
    add_to_list(new_item)                                                                                       
  File "shopping_list_3.py", line 32, in add_to_list                                                            
    position = abs(int(position))                                                                               
UnboundLocalError: local variable 'position' referenced before assignment  

I don't know about UnboundLocalError, what's the error on my code and also can you explain the what is UnboundLocalError?

Thank you.

[MOD: added ```python formatting -cf]

hello

try putting you code between ``` followed by html and then ended with ```

it is very hard to read what you have, as is. the ` is under the Esc key

thanks

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,404 Points

The else: code block in add_to_list() has a typo:

        postion = 0  # <- should be position