Bummer! You must be logged in to access this page.

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

Odd problem in Python Collections: Vending Machine Code

When running my code for the python "vending machine" I asked for chips for the first time and got this back:

Here's your chips: <built-in method pop of list object at 0x7f9f43437d08>

Oddly enough, the code then continued without an error. I've checked the code multiple times as well as made sure I saved it, but it seems to me there are no problems with it.

Here's a link to the video:

https://teamtreehouse.com/library/pop

Also, it seems to work just fine for sodas and and candy.

Hi there,

Can you post your code, please?

Steve.

Sure, here it is:

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

while True:

choice = input("Would you like 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, we don't have that!")
            continue
    except IndexError:
        print("We're all out of {}! So Sorry!".format(choice))
    else:        
        print("Here's your {}: {}".format(choice, snack))

Thanks!

1 Answer

You are missing the parentheses on your pop method for chips

snack = chips.pop

should be

snack = chips.pop()

Yikes! Can't believe I missed that! All makes sense now. Thanks!