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

Can't go out of stock

Hi I have an issue with my vending machine program... Here is a link: https://w.trhou.se/r746h3m1js And below is the code:

sodas = ["Pepsi", "Cherry Coke Zero", "Sprite"]
chips = ["Doritos", "Fritos"]
candy = ["Snickers", "M&Ms", "Twizzlers"]

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

    try:
        if choice == 'soda':
            snack = sodas.pop()
        elif choice == 'chips':
            snack = chips.pop()
        elif choice == 'candy':
            snack = candy.pop()
        else:
            print("Sorry, I didn't get that!")
            continue
    except IndexError:
        print("Sorry! We're all out of {}!".format(choice))
    else:
        print("Here's your {}: {}".format(choice,snack))

The answer I get from my console is: Would you like a SODA, some CHIPS or a CANDY? If I enter chips 3 times, I will get this response:

Sorry! We're all out of chips!
Here's your chips: Doritos

Please advice if I missed out something somewhere? Thank you!

2 Answers

Steven Parker
Steven Parker
231,141 Points

The code looks OK, and I get a different result when I run it:

Would you like a SODA, some CHIPS or a CANDY? chips
Here's your chips: Fritos
Would you like a SODA, some CHIPS or a CANDY? chips
Here's your chips: Doritos
Would you like a SODA, some CHIPS or a CANDY? chips
Sorry! We're all out of chips!
Would you like a SODA, some CHIPS or a CANDY?

Hmm, that's really weird!

Steven Parker
Steven Parker
231,141 Points

Is it possible something changed after the snapshot was made?

No, I promise no changes were made after the snapshot was taken. :/

Steven Parker
Steven Parker
231,141 Points

So is it still behaving differently?