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 Pop

Undefined variable 'snack' - any idea why?

I'm following the video. Even copied the code from teacher's download and paste it to VSC, but I'm still getting that error message Undefined variable 'snack'.

Appreciate any help!

nakalkucing
nakalkucing
12,964 Points

Hey Alim. Love to help, but it's difficult without your code. Would you mind including it? Thanks :)

3 Answers

nakalkucing
nakalkucing
12,964 Points

Hi Alim!

The Console tells you there is an Undefined variable 'snack'. The reason the console is telling you that is because you have a double equals. So snack was never defined, the computer was just told to see if snack was equal to sodas.pop(). Snack needs to equal sodas.pop(). If you look in the video you'll see Kenneth's code looks like this:

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()
    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
    print("Here's your {}: {}".format(choice, snack))

Does that clear up your problem? :) Let me know if you need any more help.

Best,

Nakal

Thanks a lot, Nakal! Have no idea how I missed that :) Thanks a lot again! everything works just fine right now!

nakalkucing
nakalkucing
12,964 Points

No problem! I'm Glad to hear it works! :)

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()
    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
    print("Here's your {}: {}".format(choice, snack))

All the words snack here in VSC are highlighted in red saying - Undefined variable 'snack'. I've literally followed the video and even copied the code from teacher's download to paste it into the VSC, but the result is the same. Here is the link to the actual video - https://teamtreehouse.com/library/pop

Thanks in advance for your help!