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

Script doesn't run in shell (python)

Whenever I create a script and try to run it in the shell I get what looks like a loop. I'll type out my code, exactly how the instructor has it, and when I go to my shell to run it, it will continue to say treehouse: -/workspace$ over and over or syntaxerror.

shopping_list []

def show_help(): 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. """)

show_help()

while True: new_item = input("> ")

if new_item == 'DONE': break elif new_item == 'HELP': show_help() continue

shopping_list.append(new_item) print("Here's your list:")

(SHELL VIEW)

treehouse: -/workspace$ python shopping_list_2.py File "shopping_list_2.py", line 5 shopping_list [] ^

SyntaxError: invalid syntax treehouse:-/workspace$

2 Answers

Haider Ali
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Haider Ali
Python Development Techdegree Graduate 24,728 Points

Variable assignment requires an = sign. You have not used = when assigning an empty list to your shopping_list variable. Also, there are a few other things wrong with your code. For example, you have not changed the user's input to upper case but in the conditional statement you wrote if new_item == 'DONE'. I have fixed your code for you and it should work how you want it to:

shopping_list = []

def show_help():
    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. ")

show_help()

while True:
    new_item = input("> ").upper()

    if new_item == 'DONE':
        break
    elif new_item == 'HELP':
        show_help()
        continue
    elif new_item == 'SHOW':
        print("Here's your list: {}".format(shopping_list))

    shopping_list.append(new_item)

Thanks, Haider.

After plugging the revised code in as my new script:

shopping_list = []

def show_help():
    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. ")

show_help()

while True:
    new_item = input("> ").upper()

    if new_item == 'DONE':
        break
    elif new_item == 'HELP':
        show_help()
        continue
    elif new_item == 'SHOW':
        print("Here's your list: {}".format(shopping_list))

    shopping_list.append(new_item)

I'm still getting the below message in the shell:

invalid syntax error. treehouse:-/workspace$ python shopping_list_2.py file "shopping_list_2.py", line 5 shopping_list [] ^