P
P
polak2282022-03-16 06:19:51
Python
polak228, 2022-03-16 06:19:51

How to print text before input() in Python?

In Python, is it possible to ask for user input with input() in the console, while simultaneously printing the text on the line BEFORE the prompt? It should look something like this:

Text 1
Text 2
Text 3
Please enter something: abc

Whenever new text is printed, it must be printed after the previous text and before the input() prompt. Also, do not interrupt user input.

So after printing "Text 4" the console should look like this:

Text 1
Text 2
Text 3
Text 4
Please enter something: abc

Is it possible to do this in Python without using any external libraries?

I already tried using \r, \b and similar codes as well as streams. I also know that I will probably need one thread to print text and I have another one asking for user input.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
P
polak228, 2022-03-22
@polak228

#-*- coding: utf-8 -*-

from threading import Thread
from time import sleep

def counter():
    i = 0
    while(True):
        sleep(0.8)
        i += 1
        print(i)

def cmd():
    while(True):
        print(f'\x1b[FCommand: {input()}')

Thread(target = counter).start()
Thread(target = cmd).start()

Looks like it's the only way :c

K
kisaa, 2022-03-16
@kisaa

But you just don't like it? Or is it necessary to remove the invitation phrase from the screen without fail?
x = input("Please enter something:")

H
HemulGM, 2022-03-16
@HemulGM

while True:
  print(text)
  x = input('enter some...:')
  text = text + x
  clr

Pseudo code where clr is clearing the console

K
Kvason, 2022-03-16
@Kvason

import os
text='text1'
for i in range(5):
    print(text)
    newtext=input('Enter something')
    text+='\n'+newtext
    os.system('clear') #либо cls для винды

V
Vindicar, 2022-03-16
@Vindicar

Either clear the terminal and re-display the contents, or use ncurses (but this is only under Linux, under Windows you need to look for one or another port).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question