user6326850
user6326850

Reputation:

Output on same line after input call

I want to get input and then print the output on that same line. This attempt doesn't work.

while True:
    op = input("Input: ")
    print("\tOutput: " + op)

The output looks like this:

Input: a
    Output:a
Input: cat
    Output cat
Input: 32
    Output: 32

I would like it to look like this:

Input: a     Output: a
Input cat    Output: cat
Input: 32    Output: 32

Is this possible in Python? It would require overwriting a line of output, but I don't know whether it's possible, or how to do it.

Upvotes: 0

Views: 1488

Answers (4)

Felix
Felix

Reputation: 6359

If you're on Windows, you can use Python's msvcrt module:

In [4]: import msvcrt
   ...: import sys
   ...: while True:
   ...:     op = ""
   ...:     print('Input: ', end='')
   ...:     while True:
   ...:         c = msvcrt.getch().decode('utf-8')  # capture single character
   ...:         if c == '\r':                       # enter pressed
   ...:             break
   ...:         op += c
   ...:         print(c, end='')                    # print character to stdout
   ...:     print('\tOutput: ' + op)                # print whole output
   ...:
Input: a        Output: a
Input: cat      Output: cat
Input: 32       Output: 32

Be careful when trying out this (or any other suggestions). If you're using an IDE, it's possible that these commands don't work in the console of the IDE. Try running the code in a standard Windows cmd.exe console and you should be fine.

Upvotes: 1

Wayne Werner
Wayne Werner

Reputation: 51807

You can't do it very easily, especially cross-platform. That's because when you press "enter" it's (surprise!) a newline.

So what you're going to have to do is move the cursor up a line. You can do this with control codes on linux - I'm not sure how to do it on Windows but it's probably awkward.

If you're on Mac/Linux, try this:

move_back = '\033[F'

choice = ''
while choice != 'q':
    choice = input('Input: ')
    print('{move_back}Input: {input_} Ouput: {output}'.format(
        move_back=move_back,
        input_=choice,
        output=choice.upper(),
    ))

The reason that you need to re-print your input is because the cursor starts at the beginning of the previous line. If you just print Output:... then it will overwrite what was already there. There are control codes to move the cursor, but it's just as easy to print out what you already have.

Upvotes: 1

Aly Mohamed
Aly Mohamed

Reputation: 9

import **sys**
sys.stdout.write() #use

You Can use "+" To concat String because The write function takes one argument

to end any line using this function you will use "\n"

Upvotes: -1

Prune
Prune

Reputation: 77847

This depends on the binding for stdout and stdin (the default output and input channels). The crux of the problem is that the user has to hit "Enter" to submit the input, and that is usually bound to a CR/LF pair. If your I/O device is enabled for moving back one line, you can print that character, reprint the input, and then print the output.

Without knowing the device or its characteristics, I can't give actual code. Check your friendly neighborhood documentation for the appropriate control characters.

Upvotes: 1

Related Questions