Python Forum
Trying to prompt user for 2 inputs on one line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to prompt user for 2 inputs on one line
#1
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
Reply
#2
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.
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Lightbulb Multiple inputs on the same line (beginner) dementshuk 9 2,712 Sep-03-2021, 02:21 PM
Last Post: dementshuk
  Automatic user/password entry on prompt by bash script PBOX_XS4_2001 3 2,726 May-18-2021, 06:42 PM
Last Post: Skaperen
  How to write a response if a user inputs a string horuscope42 3 2,184 Apr-29-2020, 03:39 PM
Last Post: deanhystad
  Perminantly saving user inputs + being able to retrieve it ThatOneGuyNoOneKnowsCodes 1 1,877 Oct-23-2019, 11:28 PM
Last Post: DT2000
  user inputs in constructing a dictionary Exsul 3 3,654 Apr-10-2019, 12:25 PM
Last Post: ichabod801
  unit testing a method that asks two user inputs() in console gebel 0 2,121 Apr-03-2019, 07:59 PM
Last Post: gebel
  question regarding user Inputs cibb 10 7,148 Apr-04-2017, 03:34 AM
Last Post: alicarlos13
  code that takes inputs for user name amounts etc and then sends custom message shaumyabrata 5 5,244 Feb-12-2017, 11:37 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020