Python Forum

Full Version: Trying to prompt user for 2 inputs on one line
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to see if I can prompt the user for two inputs at once. The code below will prompt the user for the food first and then after that
is entered another prompt will appear asking the user to enter the number of calories.

food = input('Food:')

calories = int(input('Number of Calories:'))

Is there a way to use one prompt that asks for two inputs? I would like the prompt to appear as shown below so they can enter the food next to Food: and then tab over and enter the calories next to Number of Calories:

Food: Chicken Number of Calories: 700
input returns a single string, so it's up to you how you parse that into the two values. You could, for example ask them to enter the values separated by a space and then split the string on the space to get two strings that you could then do further processing on.
Can use a loop and collect input in a list.
food_cal = []
for i in range(0,1):
    food_cal.append(input('Food: '))
    food_cal.append((input('Number of Calories: ')))

print('-'*15)
print(f'Food: {food_cal[0]} Number of Calories: {food_cal[1]}')
Output:
λ python input_loop.py Food: Apple Number of Calories: 50 --------------- Food: Apple Number of Calories: 50