+ 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
9 ответов
+ 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.
+ 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
+ 6
I guess you could shorten it a bit by using 'while clock:' for 'while clock == True:'
+ 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.
+ 5
is there an opposite for "\n"?
+ 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
+ 3
you can import sys and use sys.stdout.println() it doesnt automatically jump to next line at the end..
+ 2
I'm not sure what you mean opposite of "\n"