4

I'm using Python 3 and I'm looking for a way for the program to ask 2 user inputs in a single line.

Example of the output I'm looking for:

enter first input:                      enter second input:

However the only way I know for asking multiple user inputs is this:

first_input = input("enter first input: ")
second_input = input("enter second input: ")

#output
enter first input:
enter second input: 

Is the one I'm looking for possible? If yes, can someone teach me how to do that?

3
  • ask user to enter two numbers separated by space and then press enter.Then Split() :) Commented Aug 9, 2014 at 12:01
  • 3
    This is possible, but it requires a lot more effort; something not easy if you are beginning. Are you sure you want to do this? Commented Aug 9, 2014 at 12:24
  • hmm...i think i'll give it a shot...can you show me how with explanations in between the codes??? Commented Aug 9, 2014 at 12:31

3 Answers 3

1
choices = raw_input("Please enter two input and seperate with space")
value1, value2 = choices.split(" ")

Now if you enter 1 56 or something like this value1 will be 1 and value2 will be 56. You can choose another seperator for split function.

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

3 Comments

This doesn't do exactly what the asker wants.
This is alternative way which I know. Let him decide that.
well this just helped my friend, thanks for that...however, i'm looking for a different one
1

This is largely environment-dependent.

The following is a windows-only solution:

from ctypes import *
from ctypes import wintypes

def get_stderr_handle():
    # stdin handle is -10
    # stdout handle is -11
    # stderr handle is -12
    return windll.kernel32.GetStdHandle(-12)

def get_console_screen_buffer_info():
    csbi_ = CONSOLE_SCREEN_BUFFER_INFO()
    windll.kernel32.GetConsoleScreenBufferInfo(get_stderr_handle(), byref(csbi_))
    return csbi_

class CONSOLE_SCREEN_BUFFER_INFO(Structure):
    """struct in wincon.h."""
    _fields_ = [
    ("dwSize", wintypes._COORD),
    ("dwCursorPosition", wintypes._COORD),
    ("wAttributes", wintypes.WORD),
    ("srWindow", wintypes.SMALL_RECT),
    ("dwMaximumWindowSize", wintypes._COORD),
]

csbi = get_console_screen_buffer_info()
first_input = input("enter first input: ")
cursor_pos = csbi.dwCursorPosition
cursor_pos.X = len("enter first input: ") + len(first_input) + 1
windll.kernel32.SetConsoleCursorPosition(get_stderr_handle(), cursor_pos)
second_input = input("enter second input: ")

The following is a linux solution, which uses backspace characters. There are some implementations of get_terminal_size() here if you're using an older python version.

from shutil import get_terminal_size
first_input = input("enter first input: ")
second_input = input("\b"*(get_terminal_size()[0] - len("enter first input: ") - len(first_input) - 1) + "enter second input: ")

6 Comments

didn't work for me...the output was a series of boxes that moves when i click between them
What terminal you using, and which os?
by terminal you mean pc? ..i'm using an asus laptop with windows 7 as os ...i've got a question about your answer, is the one i'm looking for really that much dependent on the environment of my terminal; or is this 1 of the few answers i'm going to encounter?
Try the windows only solution, tested on windows 7.
the output did not change for me; i think i lack something
|
0

This may help

import sys
inputarr=sys.stdin.read().split()
print("input 1:",inputarr[0])
print("input 2:",inputarr[1])

you can use any other delimiter

notify me me if this not what you are looking for !

3 Comments

Its easy sys includes System-specific parameters and functions and stdin is the file object for interpreter’s standard input stream . sys.stdin.read() will read from standard input till EOF is reached while sts.stdin will read one line at a time .
hmm..okay..but, i didn't get the inputarr[] part
Sorry i screwed up :P it has to be sys.stdin.read().split() "sys.stdin.read()" reads the input file till EOF is reached and split() without any parameter is similar to split(" ") ie it will remove all the white-spaces and put the elements on a list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.