0

I've been trying to make a program in python that replaces the printed value in the console with the next value, in essence making a rudimentary timer.

After looking through the web for a while, I landed on this bit of code:

import time

number = 0

while True:
    print(number, end = "\r")
    number += 1
    time.sleep(1)

This would supposedly remove the previously printed number and replace it with a new one, but instead I got this output:

0123456789

video

image

What is going on?

kanuki
  • 111
  • 6
  • 2
    Share an image of your output rather than a video of it. – handlerFive Aug 28 '18 at 09:08
  • 6
    Is that IDLE? IDLE doesn't understand the `'\r'` character. Try in a terminal (although you may want to use `print(number, end='\r', flush=True)` because often the buffer waits for a newline before writing). – FHTMitchell Aug 28 '18 at 09:09
  • Possible duplicate of [How to reset cursor to the beginning of the same line in Python](https://stackoverflow.com/questions/7715594/how-to-reset-cursor-to-the-beginning-of-the-same-line-in-python) – hiro protagonist Aug 28 '18 at 09:10
  • @FHTMitchell, thanks a lot, it works in the terminal ... would have really liked to use the IDLE but I guess life is cruel. – kanuki Aug 28 '18 at 09:14
  • this is more of an opinion you dont need to use idle, as far as features go it's pretty rudimentary compared to other python editors like pycharm (which has a great free community edition version) and if you like the interactive interpreter of idle you can use IPython which is console based but again has even more features – AntiMatterDynamite Aug 28 '18 at 10:10
  • @AntiMatterDynamite thanks for the tip, I'll have a look at some of those – kanuki Aug 29 '18 at 05:22

1 Answers1

0

You can try to execute the file in the command line or the terminal.

python3 counterl.py

This gave me the expected result.

Manuel Reyes
  • 260
  • 2
  • 10