Python advanced input

I want to get some information from the user as follows:

My score 10 - already printed

Between 'is' and 'of' there is an empty space for user input, so he does not enter his information at the end (if using a simple input ()), but in the middle. While the user enters some information, he appears between 'is' and 'of'

Is there any way to do this?

+6
source share
2 answers

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

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

\x1b is an escape character. Not a single escape character is displayed on the screen; instead, they enter sequences of bytes that the terminal interprets as some kind of instruction. ESC[s tells the terminal to remember where the cursor is. ESC[u tells the terminal to move the cursor to the last saved position.

enter image description here

(A rectangle is a cursor in an unfocused window.)

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

+3
source

If you use the console, consider importing the curses library. It works with both linux and windows. Download it for windows from http://www.lfd.uci.edu/~gohlke/pythonlibs/#curses

This library has full control over the console. Here is the answer to your question. How to enter a word into the ncurses screen?

+2
source

Source: https://habr.com/ru/post/1014901/


All Articles