5

Usually, when raw_input asks you to type something in and press Return, feedback is printed on a new line. How can I print over the prompt's line? Could a CR work in this case?

Demo:

prompt = "Question: "
answer = raw_input(prompt)
print answer
print("Correct!")

Simulated output after typing an answer and pressing Return:

>> Question: my answer
>> Correct!

Desired output:

>> Correct!
octosquidopus
  • 3,517
  • 8
  • 35
  • 53
  • 2
    If on unix you can try curses - https://docs.python.org/2/howto/curses.html – Bhargav Rao Dec 04 '14 at 21:13
  • @BhargavRao I plan to learn [urwid](http://urwid.org) soon, but until then, I'm looking for a simple solution. – octosquidopus Dec 04 '14 at 21:15
  • There are options simpler in 3 than in 2 (I Guess) – Bhargav Rao Dec 04 '14 at 21:17
  • Better check this out http://stackoverflow.com/questions/4897359/output-to-the-same-line-overwriting-previous-output-python-2-5 – Bhargav Rao Dec 04 '14 at 21:24
  • 1
    @octosquidopus, see my updated answer for `urwid` example. – syntagma Dec 04 '14 at 21:24
  • @BhargavRao: `\r` won't work here because the user pressed enter (otherwise `raw_input()` won't return) i.e., you need to go *up* one line. A lightweight solution is to use ANSI codes for positioning e.g., [`blessings` package](https://pypi.python.org/pypi/blessings/) provides a simpler (than `curses`) interface (to move up, use `move_up`). `blessings` might even work on Windows with `colorama` (it needs testing). – jfs Dec 04 '14 at 22:53

2 Answers2

4

This is a solution using curses (at the end it waits for the x key to end the program):

#!/usr/bin/python                                                               

import time
import sys
import curses

def questionloop(stdscr):
    stdscr.addstr("Question: ")
    curses.echo()
    while (1):
      answer = stdscr.getstr()
      curses.flushinp()
      stdscr.clear()
      stdscr.addstr("This is correct!")
      doit = stdscr.getch()
      if doit == ord('x'):
         stdscr.addstr("Exiting!\n")
         break

curses.wrapper(questionloop)

And this is an example using urwid:

import urwid

def exit_on_q(key):
    if key in ('q', 'Q'):
        raise urwid.ExitMainLoop()

class QuestionBox(urwid.Filler):
    def keypress(self, size, key):
        if key != 'enter':
            return super(QuestionBox, self).keypress(size, key)
        self.original_widget = urwid.Text(
            u"%s\n" %
            edit.edit_text)

edit = urwid.Edit(u"Question: \n")
fill = QuestionBox(edit)
loop = urwid.MainLoop(fill, unhandled_input=exit_on_q)
loop.run()

Another (probably most concise) solution, from Veedrac's answer, would be to use blessings:

from blessings import Terminal
term = Terminal()

question = "{t.red}{}{t.normal}{t.bold}".format
answer = "{t.normal}{t.move_up}{t.green}{}{t.normal}{t.clear_eol}".format

input(question("Question: ", t=term))
print(answer("Correct!", t=term))
Community
  • 1
  • 1
syntagma
  • 23,346
  • 16
  • 78
  • 134
4

Use blessings:

from blessings import Terminal
term = Terminal()

raw_input("Question: ")
print(term.move_up() + "Correct!" + term.clear_eol())

Seriously, that's it.

Here's something fancier:

input(term.red("Question: ") + term.bold)
print(term.normal + term.move_up + term.green("Correct!") + term.clear_eol)

This shows that often calling term.thing is optional because they act similarly to properties. This means you can do awesome stuff like

from blessings import Terminal
term = Terminal()

question = "{t.red}{}{t.normal}{t.bold}".format
answer = "{t.normal}{t.move_up}{t.green}{}{t.normal}{t.clear_eol}".format

input(question("Question: ", t=term))
print(answer("Correct!", t=term))
Veedrac
  • 58,273
  • 15
  • 112
  • 169