+ 1
How to show a countdown on Python?
Is there a module that you can use to show a timer? So for example it starts with 1:00 and every second it'll go down by one? Thanks.
10 Answers
+ 4
DrChicken24 , on your computer, you can use the carriage return character \r to go to the beginning of a line and overwrite stuff. So
print("02:00", end="\r")
time.sleep(1)
print("01:59")
Should print 02:00 first, and then after one second overwrite it with 01:59. Now you just need to do this in a loop. Use floor and modulo division to get the minutes and seconds right, and a format specifier to display it like mm:ss.
Neither \r nor sleep works well here, though. And one more remark: this clock will slooowwly drift away from the exact time. We are asking it to wait one second between printing two things, but we're not accounting for the time taken to print. The drifting can be minimized with some tricks, but it's best if you just start with this đ
+ 3
import time
time.sleep()
https://www.sololearn.com/Discuss/1473687/?ref=app
+ 3
You're welcome! Let us know if you face issues writing the full code :)
+ 2
Dear drchicken24
The function to use is time.sleep
The reason you see print again is limitation of sololearn code playground. When you install Python IDE on your computer and build up interface, like in Calvin's js example for you above, then you can update the specific text box and eliminate the visual of more and more numbers showing.
I hope the above explains.
+ 1
Gordon No I mean have the actual number change from a 10 to a 9 without printing again
+ 1
Gordon Yeah that makes sense. How would you change the specific character in the string, though?
+ 1
String concantention
In Python (also javascript) , the + operator works for String combined number, auto type casting.
See this js version for illustration:
https://code.sololearn.com/WYK8Yun7Y5nI/?ref=app
+ 1
Kishalaya Saha Thanks much!
- 2
Nah Fam