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 Basics (2015) Shopping List App Continue

Kevin Ohlsson
Kevin Ohlsson
4,559 Points

How would the continue be written in python two?

Code looks like this, in Python 2.7 i get a semantic error when entering 'Done'. The program does not respect the continue line and instead includes the input 'DONE' in the list. How can i fix this?

# store items in a list and print the list program

def show_list():
    print("\n{}\n\nThese are all your items\n{}\n").format("_"*10, "_"*10)
    for item in list_database:
        print("- " + item)
    print("\n")

def help_guide():
    print("""
< Add something to a list program.
< Enter 'DONE' to exit application and show list.
< Enter 'SHOW' to show list
< Enter 'HELP' to show this help menu
    """)

def add_to_list(item):
    list_database.append(str(item))
    print("< Added '{}' to list. Stuff in list {}".format(item, len(list_database)))

list_database = []

print("""\n[SHOPBOT] Enter 'DONE' to quit the application\n""")

while True:
    user_input = raw_input(">> ")
    add_to_list(user_input)
    if user_input == 'DONE':
        break
    elif user_input == 'SHOW': #  <-------------------------- how to handle semantic error ?
        show_list()
        continue # <--------------------------------------------- does not work?
    elif user_input == 'HELP':
        help_guide()
        continue

show_list()

2 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

your continues are at the end of their respective elif blocks so they are not doing anything regardless of what version of python you are using. continue is used to skip code coming after the continue and begin the next iteration of the loop. here, since the code will not enter both elif blocks on the same trip through the loop, after executing the methods you have in your elifs, the block is finished and the next step anyway is to ask again for user input. removing the continues makes no difference. DONE is being included in the list because the first instruction after taking user input is to add the input to the list. THEN it checks to see if you have entered DONE, SHOW or HELP. instead you might put adding the input to the list at the end of the if/elif/else block, in an else block, so it would be input...if DONE...elif SHOW...elif HELP...else add to list.

Kevin Ohlsson
Kevin Ohlsson
4,559 Points

Thank you James!!

I understand that the error was due how the code was placed. Placement matters! Like, a lot!

I changed the code, now the first part of the code logic 'checks' the user input before actually doing anything, if any condition is met in the 'logic section of the code' a predetermined route is taken!

I learned that placement matters a lot. Thanks James!

Code for reference

def show_list():
    print("\n{}\n\nThese are all your items\n{}\n").format("_"*10, "_"*10)
    for item in list_database:
        print("- " + item)
    print("\n")

def help_guide():
    print("""
< Add something to a list program.
< Enter 'DONE' to exit application and show list.
< Enter 'SHOW' to show list
< Enter 'HELP' to show this help menu
    """)

def add_to_list(item):
    list_database.append(str(item))
    print("< Added '{}' to list. Stuff in list {}".format(item, len(list_database)))

list_database = []

print("""\n[SHOPBOT] Enter 'DONE' to quit the application\n""")

while True:
    user_input = raw_input(">> ")
    if user_input == 'DONE':
        break
    elif user_input == '':
        print("< Please enter something. Type 'HELP' for help.")
        continue
    elif user_input == 'SHOW': # how to handle semantic error ?
        show_list()
        continue #works!
    elif user_input == 'HELP':
        help_guide()
        continue
    add_to_list(user_input)

show_list()