I am creating a command line interface in python and I need to use a countdown and make it visible to the user, so I have coded this function below:
import sys
from time import sleep
def secondsCountdown():
remainingSeconds = 10
while (remainingSeconds >= 0):
secondsString: str = "[" + str(remainingSeconds) + " seconds]"
if (remainingSeconds > 0):
print(secondsString, end="\r") # print line and delete the previous one
sleep(1)
elif (remainingSeconds == 0):
print(secondsString) # only print line
remainingSeconds -= 1
the code itself works fine, but when the previous value is 'deleted' and the new one is printed, an extra square bracket is displayed in the new line at the end, like this example:
[9 seconds]] ## problem
[9 seconds] ## expected result
I tried to insert '\n' at the end of the 'secondsString' string but somehow this causes the code to stop deleting the previous line. I really don't know how to fix it. I hope for your help.
Thanks in advance.