Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advanced input in python

I want to receive some information from a user in a next way:

My score is of 10 - is already printed

Between 'is' and 'of' there is an empty place for user's input so he doesn't enter his information at the end( if using simple input() ) but in the middle. While user is entering some information it appears between 'is' and 'of'

Is there any possible way to do it?

like image 473
Bohdan Avatar asked Feb 11 '17 13:02

Bohdan


1 Answers

One way to get something close to what you want is if you terminal supports ANSI escape codes:

x = input("My score is \x1b[s  of 10\x1b[u")

\x1b is the escape character. Neither escape character is dipsplayed on the screen; instead, they introduce byte sequences that the terminal interprets as an instruction of some kind. ESC[s tells the terminal to remember where the cursor is at the moment. ESC[u tells the terminal to move the cursor to the last-remembered position.

enter image description here

(The rectangle is the cursor in an unfocused window.)

Using a library that abstracts away the exact terminal you are using is preferable, but this gives you an idea of how such libraries interact with your terminal: it's all just bytes written to standard output.

like image 50
chepner Avatar answered Nov 02 '22 01:11

chepner