Is there a way to receive input for multiple variables by asking the same question in Python?

I’m writing a program that categorizes refridgerator items for a final project for school. I want to ask the user to list 5 items. So far, I have this:

bev1 = input("Enter a beverage: ")

bev2 = input("Enter a beverage: ")

bev3 = input("Enter a beverage: ")

bev4 = input("Enter a beverage: ")

bev5 = input("Enter a beverage: ")

is there a way to condense this into one loop or something? I just want it to look simpler

You could use a list and append to it in a loop, instead of separate variables:

bev = list()
for i in range(5):
    bev.append(input('Enter a beverage: '))

With a generator expression you can even do that in one line, though that may more difficult to understand:

bev = [input('Give beverage: ') for i in range(5)]

And of course you could make the range size a variable and handle any number of items.

1 Like