+ 3

In python, looking for a new way to print()

I'm writing a simple clock, using the datetime function. I've got the code printing the date, time, and seconds to the output every second. it shows the the String on a new line every time. can I make this appear on the same line each time? just overwrite the old line with the new one? a sort of refreshing string. https://code.sololearn.com/cpUk9WNTU3Ec/?ref=app

10th May 2017, 5:51 PM
Chris Winner
Chris Winner - avatar
9 Réponses
+ 8
In C++, we have some sort of function to control cursor position in console in order to print stuff at different coordinates (OS based though). Not sure if Python has it.
10th May 2017, 6:15 PM
Hatsy Rei
Hatsy Rei - avatar
+ 7
You can do it with ANSI escape controls ( not compatible with code playground terminal emulator ^^ ): https://en.m.wikipedia.org/wiki/ANSI_escape_code def print_prevln(text): print("\33[1A"+str(text)) print('test') i = 0 while True: print_prevln(i) i += 1
11th May 2017, 2:07 AM
visph
visph - avatar
+ 6
I guess you could shorten it a bit by using 'while clock:' for 'while clock == True:'
11th May 2017, 1:18 AM
David Ashton
David Ashton - avatar
+ 5
i went with creating a 'clear' function as the post suggested. that kept the time visible, but it was just the illusion of a refreshing line. the terminal just kept getting "deeper" with all of the old strings just a few lines above it, out of the way. thank you for the suggestion though.
10th May 2017, 6:11 PM
Chris Winner
Chris Winner - avatar
+ 5
is there an opposite for "\n"?
10th May 2017, 7:59 PM
Chris Winner
Chris Winner - avatar
+ 3
I have hacked this together in the code below just using spacing. Spacing it far enough with print new lines I was able to move the old info off the top of the screen and fake a ball bouncing animation on a single line . doesn't animate on sololearn obviously, but if you paste it into an IDE or python it works https://code.sololearn.com/cqMqjzIi848X/?ref=app
10th May 2017, 7:33 PM
LordHill
LordHill - avatar
+ 3
you can import sys and use sys.stdout.println() it doesnt automatically jump to next line at the end..
10th May 2017, 10:26 PM
Steve
Steve - avatar
+ 2
I'm not sure what you mean opposite of "\n"
10th May 2017, 8:02 PM
LordHill
LordHill - avatar