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?

fatiherdem
  • 1,173
  • 6
  • 18
  • 35
user3921890
  • 191
  • 1
  • 9
  • ask user to enter two numbers separated by space and then press enter.Then Split() :) – vks Aug 09 '14 at 12:01
  • 3
    This is possible, but it requires [a lot more effort](https://docs.python.org/3/howto/curses.html); something not easy if you are beginning. Are you sure you want to do this? – Burhan Khalid Aug 09 '14 at 12:24
  • hmm...i think i'll give it a shot...can you show me how with explanations in between the codes??? – user3921890 Aug 09 '14 at 12:31

3 Answers3

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.

fatiherdem
  • 1,173
  • 6
  • 18
  • 35
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: ")
Community
  • 1
  • 1
simonzack
  • 19,729
  • 13
  • 73
  • 118
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 !

Namit Sinha
  • 1,415
  • 3
  • 16
  • 30
  • 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 . – Namit Sinha Aug 09 '14 at 13:15
  • hmm..okay..but, i didn't get the inputarr[] part – user3921890 Aug 09 '14 at 13:29
  • 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 – Namit Sinha Aug 09 '14 at 13:34