Can user inputs be be printed iteratively?

Can user inputs be be printed iteratively?

Problem Description:

I am taking multiple inputs from the user and trying to find the most efficient way of printing them. I know we could ask the user to input individually and then print a list like this:

list = []

for i in range(dish_no):
    item = input(f"tell us your favourite dishes: ")


But i am wondering if there is a way to take all the inputs at once and iteratively print them or append them. for example i tried:

item1, item2= input("tell us your favourite dishes: ").split()

for i in range(2):
item1, item2 = input("tell us your favourite dishes: ").split()
food_list = []

for i in range(2):

Solution – 1

You can input all dishes with a single input separating them with commas and later transform this string into a list using split(",")

items = input(f"tell us your favourite dishes separated by comma: ")

# split the string items into a list of dishes
food_list = items.split(',')

Then you can just print it as a list


Or iterate the elements and print each one individually. You can use enumerate if you want also the index so you can enumerate the elements when printing them.

for i, elem in enumerate(food_list, 1):
    print(f"{i}: {elem}")

1 Tomato Consomme and Smoked Ricotta Tortelli.
2 Quail Legs with Tamarind Glaze and Fig Chutney.
3 Slow Poached Egg with Bacon Dust & Parmesan Foam
4 Turkey Bolognese Polenta Nests
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.