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

Abdullah Jassim
Abdullah Jassim
4,551 Points

Why is it necessary to use While True? Is it possible to run without it? For me it doesnt work

soda = ["Coke", "Miranda", "Pepsi"]
chocolate = ["Kit-Kat", "Twix"]
chips = ["Lays", "Pringles", "Pofaki", "Dorritos"]


choice = input("Are you looking for soda, chocolate or chips? ").lower()

if choice == "soda":
    item = soda.pop()

elif choice == "chocolate":
    item = chocolate.pop()

elif choice == "chips":
    item = chips.pop()

elif choice == "quit":
    break

else:
    print("Sorry we dont have that. Please choose between - soda, chocolate or chips")
    continue

print("I got you a {}!".format(item)) 

2 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Abdullah,

a while loop here makes possible that the user will be asked for a drink/snack until one of the lists is empty. So if you type soda, a Pepsi will be removed/poped and printed. If you type soda again, you will get Miranda ... and so on. After the program removed Coke from the soda list and you want a soda again you will get an error, because you have an empty list. But you can ask for chocolate and get Twix and Kit-Kat. Same for chips ...

soda = ["Coke", "Miranda", "Pepsi"]
chocolate = ["Kit-Kat", "Twix"]
chips = ["Lays", "Pringles", "Pofaki", "Dorritos"]

while True:
    choice = input("Would you like a SODA, some CHOCOLATE, or a CHIPS? ").lower()
    if choice == 'soda':
        snack = sodas.pop()
    elif choice == 'chocolate':
        snack = chips.pop()
    elif choice == 'chips':
        snack = candy.pop()
    else:
        print("Sorry, I didn't understand that.")
        continue
    print("Here's your {}: {}".format(choice, snack))

Try this code.

You're right.

You don't need a while loop.

But it just so happens that it will only ask you once for a snack if you don't use any sort of loop.

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey guys, why is my answer downvoted? Is something wrong with the answer? Please specify where the error is, so I can improve it. Anyway, I would love to get some constructive critic and learn from my mistake. Thank you!