+ 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.

10th Dec 2018, 3:12 AM
DrChicken24
DrChicken24 - avatar
10 Antworten
+ 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 😊
13th Dec 2018, 3:28 AM
Kishalaya Saha
Kishalaya Saha - avatar
10th Dec 2018, 3:19 AM
Gordon
Gordon - avatar
+ 3
You're welcome! Let us know if you face issues writing the full code :)
14th Dec 2018, 2:53 AM
Kishalaya Saha
Kishalaya Saha - avatar
10th Dec 2018, 5:31 AM
Calviղ
Calviղ - avatar
+ 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.
13th Dec 2018, 1:28 AM
Gordon
Gordon - avatar
+ 1
Gordon No I mean have the actual number change from a 10 to a 9 without printing again
13th Dec 2018, 1:04 AM
DrChicken24
DrChicken24 - avatar
+ 1
Gordon Yeah that makes sense. How would you change the specific character in the string, though?
13th Dec 2018, 1:33 AM
DrChicken24
DrChicken24 - avatar
+ 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
13th Dec 2018, 1:41 AM
Gordon
Gordon - avatar
+ 1
Kishalaya Saha Thanks much!
14th Dec 2018, 2:30 AM
DrChicken24
DrChicken24 - avatar
- 2
Nah Fam
10th Dec 2018, 10:43 PM
Nick
Nick - avatar