Countdown timer program in Python ⌛

preview_player
Показать описание
#python #tutorial #course

import time

my_time = int(input("Enter the time in seconds: "))

for x in range(my_time, 0, -1):
seconds = x % 60
minutes = int(x / 60) % 60
hours = int(x / 3600)
print(f"{hours:02}:{minutes:02}:{seconds:02}")

print("TIME'S UP!")
Рекомендации по теме
Комментарии
Автор

import time

my_time = int(input("Enter the time in seconds: "))

for x in range(my_time, 0, -1):
seconds = x % 60
minutes = int(x / 60) % 60
hours = int(x / 3600)

time.sleep(1)

print("TIME'S UP!")

BroCodez
Автор

Another amazing video, thank Bro code.

The real beginner explanation for the x % 60 for seconds is because the way modulo works is, for example, if a user enters 30 seconds then it would be 30 % 60 and what is the remainder? 30. You can't think of it as division, but rather how many times does 60 fit into 30? Zero, but we still have a remainder of 30. What about 60 % 60? 60 fits into 60 once and we have a remainder of 0. What about 61 % 60? 60 fits into 61 once and we have a remainder of 1. The reason why % 60 is being used, is because in terms of time, on the seconds hand, we count only up to 59 and 60 turns into 1 minute or :00. So if a user inputs 61 we don't want the program to show us 00:00:61 because there's no such thing as 61 seconds when it comes to time; it's 00:01:01(1 minute and 1 second). So if a user enters 61, then 61 % 60 will come out as :01 which is correct. Then probably the beginner will ask, so where does the minute go then? the minute is being calculated with the (x / 60) % 60. However, without the int() cast you will get a number with some decimals and we don't want that for time. If you run (61 / 60) % 60 you will get something like So when we int() cast it with int((61 / 60) % 60)) we get a 1 instead, which is 1minute. So for seconds we get 1 and for minutes we get 1 when the user inputs 61. 00:01:01. For hour same thing.
I hope this helps some real beginners.

tigerex
Автор

Your teaching pace is fantastic. Great instruction video. Thank you

Homnuangirl
Автор

Dude I’ve been searching for this for like two hours to make a speedrun timer for my game.. I knew how to get the seconds (because the time is already in seconds), and the minutes (just divide the seconds by 60), but the ten seconds place and ten minutes place had me stumped. I didn’t even think about using the “x % 60” thing. Thank you so much

ianboyer
Автор

I watched this in the 12 hour lesson you have but forgot how to use it now that I’m doing my first project and needed a reminder, love your videos!

thassadub
Автор

This is by far the simplest way to make a timer in python, so simple and elegant. That thing with making an int before % is genious. Im subscribing! BTW - I also realised I did know how modulo behaves when divisor is smaller then dividend.

markotomasevic
Автор

This Channel deserve 10 million subscribers

kunalkamthe
Автор

### using while loop

import time

countdown_time = int(input("Enter a time in seconds: "))

while countdown_time >= 0:
seconds = countdown_time % 60
minutes = int(countdown_time / 60) % 60
hours = int(countdown_time / 3600) % 24



time.sleep(1)
countdown_time -= 1

print("Time's up!")

hosseinrezaei
Автор

I need a count-down timer starting at 112 minutes (7.5 work hours divided into quarter-day segments). The timer must include a pause function. The current time at pause time must be stored such that if the program is closed and reopened the timer will be initiated at the last-stored time. An audio file is to be sounded at time=0. For a solution, I will consult with Google's AI studio, but will defer to any assistance or resource references that may be provided here (human interaction is worthy of support). Thank you all, and Mr. Bro Code, your tutorials are superlative and very much appreciated.

jwf
Автор

Please upload more creative and challenging programs in python.please don't stop python videos 🙏✨😊

myeducationvlogs
Автор

to make it proper like a timer you can add something like print (f"{hrs}:{mins}:{secs}", end="\r")
you'll see that print statements won't pile up and just update the values and stays where they are

ru_uwu
Автор

my code editor suggested:

for i in range(alarm_time, 0, -1):
seconds = i % 60
minutes = i // 60 % 60
hours = i // 3600
print(f"{hours:02d}:{minutes:02d}:{seconds:02d}", end="\r")

Didn't know what "//" was so looked it up: floor division. Returns the "quotient" (aka how many times the thing we're dividing goes into the thing we're dividing into). Returns an integer so no need to declare type (eg. just "i" instead of "int(i)")

this is, apparently, lighter and less error prone:

"Floor Division (//) is generally better for this use case because:
It's faster and more direct (avoids creating a float).
It reduces the chances of unexpected precision errors with floating-point arithmetic."

Hope this helps.

(Oh and " end="\r" keeps it on one line)

koch
Автор

Great explanation🎉
To make it better add "\r" in print statement.
It will look like :
print(f"string", "\r")

KunjMandaviya
Автор

that was a great exercise sir thank you so much

walidkhaled
Автор

Thanks bro for sharing this. I love the way you teach.

sufiyanshaikh
Автор

What a great content here. Thank you Bro 🤩

kapibara
Автор

How great code I really love this channel, keep up the good work

EfemeRoyal
Автор

Wow ha so much fun with this :) Thanks Bro!!

zaydarendse
Автор

#pythoncountdowntimer :
import time
print("Hi, I'm a Countdown Timer⌛.")
my_time = int(input("Enter the time in seconds :"))
for x in reversed(range(0, my_time)) :
seconds = x % 60
minutes = int(x / 60) % 60
hours = int(x / 3600)

time.sleep(1)
print("TIME'S UP!")

extendedsomeone
Автор

before him, i tried making my own timer. It worked but it's not like this one
this is how it goes

import time

timer = int(input("how much to set for timer"))
for x in range(timer):
print(timer)
time.sleep(1)
timer = timer - 1

ahura
join shbcf.ru