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

pop, python collections: console

I try to run the code that is in the console, but Python just creates a new blank line of code without running anything

https://w.trhou.se/404qp44b23

Jeff Muday
Jeff Muday
Treehouse Moderator 28,732 Points

There are three programs at that URL, but one is entirely blank (shopping_list.py2). Perhaps you forgot to save it?

The other two (Shopping_list.py and Vending_machine.py) look like they'd run/print to the console.

1 Answer

Hey Emilio! You don't have the second part of your try/except block. The except portion should catch an IndexError, for when your list of snacks, drinks, or candies has run out of items. Also, your if statements should be indented under the try:. Additionally, it's nice to add some sort of way to break out of the loop.

candy = ["Chocolate", "Chilli", "Caramel"]
sodas = ["Coke", "Sprite", "fanta"]
chips = ["Lays", "doritos", "salt"]

while True:
    choice = input("would you like a SODA, some CHIPS, or a CANDY? ").lower()

    try:
        if choice == 'done':
            break
        if choice == 'soda':
            snack = sodas.pop()
        elif choice == 'chips':
            snack = chips.pop()
        elif choice == 'candy':
            snack = candy.pop()
        else:
            print("Sorry, i didn't understand that.")
            continue
    except IndexError:
        print('Sorry, we're out of {}'.format(choice))
    else:
        print("Here's your {}: {}".format(choice, snack))

Hopefully this clears it up for ya!