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

srikanth soma
srikanth soma
1,572 Points

Shopping list what is wrong in my code when I run the file I get the below error I'm using pycharm

#Creating a shopping list
shopping_list = []
# SHOW for list of items in the list
def show_items():
    for items in shopping_list:
        print(items)

#HELP for showing special commands
def help():
    print ("DONE for quitting adding to the list")
    print ("HELP for help information")
    print ("SHOW for list of items in the list")
def add_items():
    shopping_list.append(user_input)

while True:
    # Take user input and add items to the list
    user_input = input("Enter the items : ")
    add_items()
    if user_input == 'SHOW':
        show_items()
        continue
    elif user_input == 'HELP':
        help()
        continue
    # DONE for quitting app
    elif user_input == 'DONE':
        break
    add_items()
Enter the items : dkjasfk
Traceback (most recent call last):
  File "C:/Users/Somasx/PycharmProjects/Treehouse/Shopping-List.py", line 20, in <module>
    user_input = input("Enter the items : ")
  File "<string>", line 1, in <module>
NameError: name 'dkjasfk' is not defined

3 Answers

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

I'm not 100% sure, but I think there's missing a argument in the add_items() + I added an else statement otherwise the add_items() will be called every time there is a user input :-)

#Creating a shopping list
shopping_list = []
# SHOW for list of items in the list
def show_items():
    for items in shopping_list:
        print(items)

#HELP for showing special commands
def help():
    print ("DONE for quitting adding to the list")
    print ("HELP for help information")
    print ("SHOW for list of items in the list")
def add_items(user_input):  # added an argument to the function
    shopping_list.append(user_input)

while True:
    # Take user input and add items to the list
    user_input = input("Enter the items : ")

    # removing add_items()

    if user_input == 'SHOW':
        show_items()
        continue
    elif user_input == 'HELP':
        help()
        continue
    # DONE for quitting app
    elif user_input == 'DONE':
        break
    else:  # added an else statement
        add_items(user_input)
srikanth soma
srikanth soma
1,572 Points

I copied and pasted your code and I still see the same error

srikanth soma
srikanth soma
1,572 Points

I found the reason for the error I'm using Python 2.7 and I don't know that I've to use raw_input after the input to raw_input fix the issue