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 Shopping List Take Three

Azizi Ali
PLUS
Azizi Ali
Courses Plus Student 1,049 Points

I got NameError while inserting item in the list

Hi, I can run the module in the python shell, but when I insert item such as "apples" and press Enter, I got NameError like this:

NameError: name 'item' is not defined

This NameError happen in "shopping_list.insert(position-1, item)"

Below is my code:

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.
""")

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 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(item))
    else:
        position = 0

    try:
        position = abs(int(position))
    except ValueError:
        position = None
    if position is not None:
        # NameError happen here (maybe, if I'm not wrong)
        shopping_list.insert(position-1, item)  
    else:
        shopping_list.append(new_item)

    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
    else:
        add_to_list(new_item)

show_list()

Can anyone help me to solve this? Thank you, guys :)

3 Answers

Item is the counter variable inside of this for loop " for item in shopping_list:", its scope is only inside that function or loop

However you have item in the function "add_to_list" and it is not declared, ".format(item)"

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

I have the same error and I still don't understand it could you please explain again?

On the add to list function...in the script...instead of using the variable item... make it new_item ( in the add to list function)

i said item..but item isnt declared in the scope of add_tolist function... so everytime you see item int he function add_tolist.. just make it new_item... since that is what is being passed.