3

I'm trying to create a very simple program in python that needs to read input from the user and write output accordingly. I need an output similar to this:

$./program.py
say something: Hello World
result: hello world

The thing is that i need to read input indefinitely, each time the user inputs data i would like that the printed data doesn't obstruct the input prompt. It will even better that no newlines be printed, keeping the output as above: a line for reading and another for writing.

I tried using curses but i don't want the hole screen to be used, just the two lines.

1
  • Each time a character is printed in the first line, it should be echoed in the second line? Commented Oct 5, 2016 at 14:26

2 Answers 2

3

I believe this is what you want:

import colorama
colorama.init()
no = 0
while True:
    user_input = str(raw_input('\033[2A'*no + '\033[KSay something: '))
    print '\033[KResult: ' + user_input
    no = 1

This how it looks after entering the string:

Working solution

This implementation works on windows, however, if you use Linux, if I am not mistaken these are not necessary:

import colorama
colorama.init() 

EDIT: Modified my code a bit so it does not overwrite the text that was printed before the execution of the code. Also added an image of working implementation.

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

Comments

2

You can do veeeery simple trick:

from os import system
while True:
    system('clear')  # or 'cls' if you are running windows
    user_input = input('say something:')
    print('result: ' + user_input)
    input()

5 Comments

This will clear the screen immediately after printing the result, so you will never see it
Won't this "update" the screen only after Enter key be pressed?
Sure it works, but I wonder if OP wants to update the screen on every "key got pressed event". If it's the case, it would be a little harder to do.
KISS :) Let's worry about it later. If this is not the solution OP is looking for, we will find something more complicated.
the problem is that clearing the screen is the exact opposite to what i'm looking for. I would like to clear only the used two lines.

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.