Display previous input on keyup from user, in Python?

Question:

I have a simple Python program which uses a read-eval-print loop to read user input via raw_input and then print things to the screen. I would like to keep a history of previous inputs and cycle through them when the user presses keyup or keydown, similar to the Python interpreter or to the bash shell. How can I do this in Python?

Someone asked for sample code:

while True:
    user_input = raw_input()
    print user_input + " this many hats!!!"

I’d like to make it so a keyup puts the last line of input on the command line. The first answer given, use the readline module, is likely the best.

Asked By: Kevin Burke

||

Answers:

Try using the readline module. If your platform supports readline, simply importing the module should make its functionality available via the raw_input prompt.

Answered By: Josh Rosen

For standard input function also works. No configuration needed. Just import readline.

Answered By: Anton Samokat
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.