user3921890
user3921890

Reputation: 191

Ask multiple user input in single line

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?

Upvotes: 4

Views: 598

Answers (3)

Namit Sinha
Namit Sinha

Reputation: 1445

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 !

Upvotes: 0

simonzack
simonzack

Reputation: 20948

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: ")

Upvotes: 1

fatiherdem
fatiherdem

Reputation: 1233

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.

Upvotes: 1

Related Questions