1

I have a simple python code where i printed a question and i expect input from the user. I imported readline so that user can access to history of his inputs and use arrow keys. This is the code :

import readline

print("Welcome to Numgame, please enter a number \nuser $", end=" ")
m = input()

After running the script, if i add one character and press backspace to remove the character all of the second line is deleted.

Example: This is the output of running the script and typing for example 'e' :

Welcome to Numgame, please enter a number

user $ e

If i press backspace, the output looks like this:

Welcome to Numgame, please enter a number

As you can see, the second line is gone. I want a solution so that i can use backspace without "user $" being removed.

  • Try `m = input("user $ ")`? – Lou Dec 09 '22 at 10:22
  • Having said that, I can't reproduce your bug, even when I run your program directly. Can you tell us more about how you're running the script, what environment? (Linux terminal, Windows, Git bash, WSL, python REPL etc.?) – Lou Dec 09 '22 at 10:24
  • The solution of using `input()` works very well but i am going to use some special print like rich.print function to color the text before '$' that's why i wanted to make input empty and print all the sentences using print() – John Kennedy. Tech Dec 09 '22 at 10:46
  • Okay. You can do that using `input()` too. You can put ANSI escape codes directly into the string - see [this answer](https://stackoverflow.com/a/287944/1907765) for more details. E.g. `input("\033[94muser $ \033[0m")`. You can store those escape codes in a variable, and then put them inside the input string to make the code more readable also. – Lou Dec 09 '22 at 10:52
  • Thanks, the tip for using ANSI escape codes is very useful. It works very well. – John Kennedy. Tech Dec 09 '22 at 11:18

0 Answers0