0

I have a program asking user for answers to some riddles and I'd like to implement of stopwatch of sorts. Is it possible to count from 0 to 30, but to make seconds update "over each other", so that it doesn't display:

01
02
03
...

or

01 02 03 ...

but overwrites the same two digits every time. I hope I'm making myself clear.

Here is what I have so far:

        start = time.time()
        elapsed = 0
        while elapsed < 30:
            elapsed = time.time() - start
            print "%02d" % elapsed
            time.sleep(1)

But this prints the seconds in a column. I'd like to display it as I've described above so that prompt "Your answer: " is always in the same place.

Luka
  • 143
  • 3
  • 12
  • possible duplicate of [Replace console output in python](http://stackoverflow.com/questions/6169217/replace-console-output-in-python) – jonrsharpe Jul 27 '15 at 14:08
  • possible duplicate of [Output to the same line overwriting previous](http://stackoverflow.com/questions/26584003/output-to-the-same-line-overwriting-previous) – tobias_k Jul 27 '15 at 14:10
  • it is hard in console to do that , you must use shell specific command or libs. like ncurses. – nkcode Jul 27 '15 at 14:11
  • @jonrsharpe I managed to make seconds update in same line and overwrite each other, but asking user for input messes that up, as expected. – Luka Jul 27 '15 at 14:29

1 Answers1

0

If this program output in the command line it won't be possible to overwrite the digits. Command line output is not set up to be dynamic.

Right now you seem to be inputing and printing variables through the command line. Once you are comfortable with python, you might want to look into using a framework ( What is a Framework?) such as Django for your python projects. Instead of having the user interface of your program limited to the command line, you will be able to create an application which could have the timer updating in one location.

If your user interface does not happen to be the command line, you should move your print statement outside of the while loop and implement the print somewhere else.

Community
  • 1
  • 1
Chris Cerk
  • 41
  • 8