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 trialAlim Nastaev
15,838 PointsUndefined 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!
3 Answers
nakalkucing
12,964 PointsHi 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
Alim Nastaev
15,838 PointsThanks a lot, Nakal! Have no idea how I missed that :) Thanks a lot again! everything works just fine right now!
nakalkucing
12,964 PointsNo problem! I'm Glad to hear it works! :)
Alim Nastaev
15,838 Pointssodas = ["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!
nakalkucing
12,964 Pointsnakalkucing
12,964 PointsHey Alim. Love to help, but it's difficult without your code. Would you mind including it? Thanks :)