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

Nikhil Alexander
Nikhil Alexander
1,444 Points

im getting a syntax error which is saying that index += 1 is an invalid syntax

index += 1 is giving me a syntax error please help me out

1 Answer

Steven Parker
Steven Parker
231,248 Points

Have you previously created "index" and given it a value? If not, this code would cause a syntax error because it's trying to add one to something that doesn't exist yet.

If that's not the problem, you might need to show the whole program. If it's short, you can post it (with proper formatting). Even better, make a snapshot of your workspace and post the link to it here.

Nikhil Alexander
Nikhil Alexander
1,444 Points

sorry i forgot to attach my script... i have created index...can u please help me out??

import os

#make a list
fruits_list = []

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


#print instructions
def help_list():
    clear_screen()
    print("Type your list in here")
    print("""
typing DONE completes the list
typing SHOW shows your current list
typing HELP shows the above instructions again""")

help_list()

def show_list():
    clear_screen()

    print("Here's your list: ")

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

    print("-"*10)          

def added_items(item):
    #add new items to the list
    show_list()
    if len(shopping_list):
        position = input("Where should I put {}?\n"
                         "Press ENTER after typing the serial number."
                         "> ".format(item))
    else:
        position = 0

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

    if position is not None:
        fruits_list.insert(position-1, item)
    else:
        fruits_list.append(new_item)

    show_list()

#ask for new items
while True:
    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":
        help_list()
        continue

    elif new_item.upper() == "SHOW":
        show_list()
        continue

    else:    
        added_items(new_item)   

show_list()    
Steven Parker
Steven Parker
231,248 Points

The issue is on the previous line:

        print("{}. {}".format(index, item)

The parentheses are unbalanced — there are two opens but only one closing parenthesis.

Nikhil Alexander
Nikhil Alexander
1,444 Points

thank you for the help Steven ... i also rectified all my other mistakes in the script ... thank you once again ...