2

When using raw_input in python, a user has to put an input and then press enter. Is there a way for me to code something, where the code will prompt several user inputs at the same time and THEN press enter for the code to run?

For example: instead of ...

>>> Name: <prompt> <enter>
>>> Age: <prompt> <enter>
>>> Gender: <prompt> <enter>

it'll have ...

>>>
Name: <prompt>
Age: <prompt>
Gender: <prompt>
<enter>
3
  • You may want to alter the way you ingest data entirely if you're running a script with multiple parameters or options. The argparse package is good for passing options to a script using command line flags. You could also leverage something like the json or configparser modules to ingest these paramters from a file. Commented Mar 16, 2018 at 17:05
  • input is meant to be very simple and minimal. If you want more advanced user interaction, there are many ways to create a user interface, depending on your target environment. Do you want a text terminal application, a graphical user interface or maybe a web application? Commented Mar 16, 2018 at 17:24
  • @PedroSerrano, if one of the below solutions helped, consider accepting it (green tick on left). Commented Mar 22, 2018 at 13:16

3 Answers 3

4

No, input() only allows one string input to be supplied.

What you can do is this:

name, age, gender = input('Enter Name|Age|Gender:').split('|')

# user inputs "ABC|30|M"

print(name, age, gender)
# ABC 30 M

Now you just rely on user not having a | character in their name.

Or, of course, you can ask separate questions.

Sign up to request clarification or add additional context in comments.

Comments

0

One way is to do like this:

x=raw_input("Enter values: ")
a=x.split(' ')

And now you have the separate values in a.

Demonstration:

>>> x=raw_input("Enter values: ")
Enter values: 12 65 hello
>>> a=x.split(' ')
>>> a
['12', '65', 'hello']

Then you could do age=a[0] and so forth.

You could use the split directly like this:

a,b,c=raw_input("Enter values: ").split(' ')

One downside is that if the user does not give three values, you will get an error. If you split afterwards you can perform controls on the input first.

Note:

This method may cause problem if you want spaces in data, since space is used as a delimiter. Switch to another token in split() to allow spaces if needed.

Comments

0

If name will always be letters, age will always be numbers and gender will always be letters, and they are in that order, this should work:

info=input("what is your name, age and gender?")
name=""
age=""
gender=""
var="n"
for i in info:
    if var=="n":
        try:
            int(i)
            var="a"
        except:
            name+=i
    if var=="a":
        try:
            int(i)
            age+=i
        except:
            var="g"
    if var=="g":
        gender+=i
print(name, age, gender)

It means that you won't have to worry about the user inputing a seperator.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.