Python Forum
Print on the same position - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Print on the same position (/thread-14499.html)



Print on the same position - Marv - Dec-03-2018

Hello all,
I am new in Python coding, but I would like to ask for help. I need print data on same line and same position so values will be overwritten (for example counter of seconds)
I've had this code:

import time
for n in range(1, 6):
print(' time is 0.{}sec'.format(n), end='\r')
time.sleep(0.1)


but after runnig of code have all data on one line but they are not owerwritten.

time is 0.1sec time is 0.2sec time is 0.3sec time is 0.4sec time is 0.5sec

Can you help me where can be a problem?
I 've had Python 3.7.1 (Windows).
Thank you very much.
Marv


RE: Print on the same position - nilamo - Dec-03-2018

What version of windows are you using? Are you running it from the console, or an ide?

I'm on Windows 10, with python 3.7.0, and your code works as expected:
>>> import time
>>> for n in range(5):
...   print(f"time is {n}", end="\r")
...   time.sleep(0.1)
... else:
...   print()
...
time is 4
>>>



RE: Print on the same position - Marv - Dec-03-2018

Thanks, it is strange that it works on your computer.
I have Win 10, and running from IDLE


RE: Print on the same position - nilamo - Dec-03-2018

Try it from the console. IDLE does some weird things, and that very well could be affecting the results.


RE: Print on the same position - Marv - Dec-03-2018

Hi nilamo,
thanks for your tip. In the console it works. I am little bit confusing why not in IDLE:-).
take care...


RE: Print on the same position - nilamo - Dec-03-2018

Idle is pretty bad. I wish it wasn't included with python. It's got so many weird little issues, that telling people they should be using it for development is just wrong. I can't remember, but I think it also has an issue where trying to use tkinter causes idle to freeze and need to be force-quit.