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 trialVincent Cegers
1,248 Pointswont print final else statement
I can't get it to print my final else statement. what am I doing wrong
soda = ['coke','pepsi','dew'] chips = ['fritos', 'doritos'] candy = ['twizzler', 'starburst', 'twix']
while True: choice = input("would you like a SODA, CHIPS, or a CANDY? ").lower()
try:
if choice == 'soda':
snack = soda.pop()
elif choice == 'chips':
snack = chips.pop()
elif choice == 'candy':
snack = candy.pop()
else:
print("sorry i didnt understand that.")
continue
except IndexError:
print("sorry all out of {}".format(choice))
else:
print("here is your {}: {}".format(choice, snack))
2 Answers
Jonathan Grieve
Treehouse Moderator 91,253 PointsI've just run your code on a local file on my system and the script is working perfectly for me.
Try typing anything that doesn't appear in any of your arrays.
Mikael D.D
Courses Plus Student 2,080 PointsHi Vincent, my guess is that the program will never reach that last line: the else statement...After the continue statement it jumps back at the top of the loop and if it throws a IndexError it will hande it by printing "sorry all out of {itemname}" then it will go back at the top of the loop again never reaching that last Else statement.
I copied and pasted your code : All I did is delete the else: key word at the end of your code, then I moved up your print("here is your {}: {}".format(choice, snack)) in the try block under the continue line.
I would love to have another opinion on this as I'm no expert...Yet...Maybe one day ;) But I'm willing to bet 10 myrrh that last else statement will never see daylight if you leave it there. It will jump right back in that loop and never run that line.
Here's your code so you can see what I did.
soda = ['coke','pepsi','dew']
chips = ['fritos', 'doritos']
candy = ['twizzler', 'starburst', 'twix']
while True:
choice = input("would you like a SODA, CHIPS, or a CANDY? ").lower()
try:
if choice == 'soda':
snack = soda.pop()
elif choice == 'chips':
snack = chips.pop()
elif choice == 'candy':
snack = candy.pop()
else:
print("sorry i didnt understand that.")
continue
print("here is your {}: {}".format(choice, snack))
except IndexError:
print("sorry all out of {}".format(choice))
Vincent Cegers
1,248 PointsVincent Cegers
1,248 Pointshmmm still doesn't work in my workspace. odd. thank you for the check