Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print on the same position
#1
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
Reply
#2
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
>>>
Reply
#3
Thanks, it is strange that it works on your computer.
I have Win 10, and running from IDLE
Reply
#4
Try it from the console. IDLE does some weird things, and that very well could be affecting the results.
Reply
#5
Hi nilamo,
thanks for your tip. In the console it works. I am little bit confusing why not in IDLE:-).
take care...
Reply
#6
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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020