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: ")
list.append(item)
print(list)
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):
print(item{i})
item1, item2 = input("tell us your favourite dishes: ").split()
food_list = []
for i in range(2):
food_list.append(item{i})
CodePudding user response:
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
print(food_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