Countdown timer program in Python ⌛
HTML-код
- Опубликовано: 8 сен 2024
- #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}")
time.sleep(1)
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)
print(f"{hours:02}:{minutes:02}:{seconds:02}")
time.sleep(1)
print("TIME'S UP!")
can you make this same video but using WHILE LOOP ??
@@shiningstar9403 yes
while my_time > 0:
you'll need to manually decrement my_time by one during each iteration tho
How is this converted into a background thread Bro?
@@BroCodez hi can you make it in java with gui
Hey Bro Code, what do the “:02” between minute and houre and second do. Why we to add the :02
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 1.0166666~. 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.
This helped so much thank you
Yeah I also stucked there only 😑but your comment helps very much to under stand 🎉
damn what a helpful explanation. Brocode should pin your comment
The explanation of why x % 60 :
Modulo is a concept from math thats:
a mod b is the remainder of the division of a by b.
If b > a > 0 then a mod b = a
So, 5 % 3 = 2
And 11 % 60 = 11.
Just love this human being.
Please do not bomb him laden's son
import time
hours = int(input("Enter the number of hours: "))
minutes = int(input("Enter the number of minutes: "))
seconds = int(input("Enter the number of seconds: "))
total_seconds = hours * 3600 + minutes * 60 + seconds
for i in range(total_seconds, 0, -1):
hours = int(i / 3600)
minutes = int(i / 60) % 60
seconds = i % 60
print(f"{hours:02d}:{minutes:02d}:{seconds:02d}")
time.sleep(1)
print("Time's up!")
#check this out
We all do!
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
yo bro can you please explain to me why did he use that % thing and how it got him to right amount of second it should appear Im trying hard and I don't get it
@@MoreBilaINoFilter if for example you have 3700 seconds which is 1 hour 1 minute and 40 seconds, with %60. Seconds is 40 (3700 %60=40). THIS SIGN % CALCULATES THE REMAINDER OF THE DIVISION. So minutes is 1 (3700/60 = 61) . Bcs 61 is not acceptible to have in the timer you add % 60 so it will be 61 % 60 = 1 (THE REMAINDER)
Your teaching pace is fantastic. Great instruction video. Thank you
to make it proper like a timer you can add something like print (f"{hrs}:{mins}:{secs}", end="
")
you'll see that print statements won't pile up and just update the values and stays where they are
brother can you help me? i didn't get the minute variable. why is minutes = int(x / 60) % 60 ? if x is 65 then 65 / 60 is about 1.08 and what is 1.08 % 60 ???
@@noliyeb Hello, it might be late to answer and you probably already know, but I will explain:
When you put int before a parenthesis, you are asking the computer to ignore the fractions/floats/numbers with a point (.) on it. So in the case you mentioned it, 65/60, the answer is 1.08, but int(65/60) equals 1 because the int makes so that the computer ignores the 0.08 remaining. Notice that if you remove the int it will result in 1.08, because the computer will show the real result instead of your preferred one (in int).
The one last thing is you asked what is 1.08 % 60:
The answer is 0. When you use the %, the result will be the remainder of the division of the numbers.
Imagine that you have $65.00 dollars, and divide this by 60 people without giving them cents. You can only give your money if you have a number with no fractions, that's what int(integer) stands for, and it's the form of digit that the % will use. Can you do this and still have any money to spare? Yes, because you can give $1.00 to each and the REMAINDER will be 5, because 60 / 60 = 1 , and you can't divide 5 (from 65) by 60 with a integer result .
I tried that and my string ended up disappearing, i ended up using this code and it worked for me, lol:
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(end="
" f"{hours:02}:{minutes:02}:{seconds:02}")
time.sleep(1)
print("
Time's UP!")
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!
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.
Please upload more creative and challenging programs in python.please don't stop python videos 🙏✨😊
This Channel deserve 10 million subscribers
Great explanation🎉
To make it better add "
" in print statement.
It will look like :
print(f"string", "
")
Your suggestion does nothing for me. I am confused. I literally just added what you told us to add. Does not change the output in any way.
brother can you help me? i didn't get the minute variable. why is minutes = int(x / 60) % 60 ? if x is 65 then 65 / 60 is about 1.08 and what is 1.08 % 60 ???
It turns out putting an operation within a function int() for example int(3600/3605) if you print this you will get the answer without the remainder which is equal to 1 why is that? because integers doesn't have decimal which floats have that's why to get rid of the remainder bro used int() function and to ensure that minutes and seconds does not exceed to 60 he used modulo operator(%) which will give only the remainder of that number. For example we have 75 minutes, we will use modulo operator (75 % 60) to convert it to 15 minutes and where will the exceeding 60 min go? the time function or variable or whatever you call that will take care of that. Because here we used one variable(user input) and used it to form 3 new variables (hours, minutes and seconds) or whatever that is called.
Suppose the user entered 3665 which is equivalent to 1 hr 1 min and 5 secs. How we will be able to convert that into hours, minutes and then count backwards for each of the three variables to make it look like a timer? I think this is what this exercise is about.
So far that is my understanding in this excercise a simple tip is to try code by yourself before watching this video and compare your's and bro's code, which is better? I always try to code by myself before watching bro's exercises I just look what it is all about and try to code, it really helps my problem solving skills and logic to grow.
In conclusion, I think this is the best playlist to learn python it has challenging exercises and clear explanation of the topics.
Thank you for your lessons it's really true that not all heroes wear capes
ps.I tried to code this exercise and got 36 lines while this man just takes 12 lines Gigachad.
but how is extra seconds moving into minutes????
i mean if i put 70 seconds my pc is doing 10 seconds first then 70 it aint going into minutes
brother can you help me? i didn't get the minute variable. why is minutes = int(x / 60) % 60 ? if x is 65 then 65 / 60 is about 1.08 and what is 1.08 % 60 ???
Understand this concept below:
a mod b is the remainder of the division of a by b.
If b > a > 0 then a mod b = a
So, 5 % 3 = 2
And 11 % 60 = 11.
Hope thats clear enough
Thanks bro for sharing this. I love the way you teach.
I got it, I fcking got it, i needed 5 minutes to comprehend all magic, thank you bro
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
Thank you. New Subscriber.
Bro thank u soo much u saved my life literally ❤
I tried to use a "while True" loop with a lot of "if statements" because I thought it could work better than a "for loop", but it doesn't. There isn't a way to stop it at 00: 00: 00. It always stops running at 00:00:-1.
Here is the main structure. A classic one.
while True:
seconds -= 1
if seconds == 0:
minutes -= 1
seconds = 59
if minutes == 0:
hours -= 1
minutes = 59
elif seconds == x and minutes == 0 and hours == 0:
time.sleep(1)
break
If you try seconds == 0 or seconds == -1 it won't stop
Instead of range function, I used while loops and it worked lol:
# timer:
import time
import colorama
from colorama import Fore
colorama.init()
x = int(input('Number: '))
while x != 0:
second = x % 60
minute = int(x / 60 ) % 60
hour = int(x / 3600 )
print(Fore.GREEN + f'{hour:02}:{minute:02}:{second:02}')
x = x-1
time.sleep(1)
print(Fore.RED + 'Process is finished. ')
brother can you help me? i didn't get the minute variable. why is minutes = int(x / 60) % 60 ? if x is 65 then 65 / 60 is about 1.08 and what is 1.08 % 60 ???
that was a great exercise sir thank you so much
marvelous content.
Hvgv
Very cool! I am sure there will be a way for the output , every time to replace the previous output, so all the results will be in one line only
yes there is , just dont print the f string directly instead put it in a variable and then print the variable and end="
"
import time
my_time = int(input("Enter the time for timer in seconds : "))
for x in reversed(range(0, my_time + 1)):
seconds = x % 60
miniutes = int(x/60) % 60
hours = int(x/3600) % 60
a = f"{hours:02}.{miniutes:02}.{seconds:02}"
print(a , end="
")
time.sleep(1)
print("Times up!")
hope that helps
chat gpt used in good way hahahahahah
@@POWER-WAIFU
It worked🎉. My frist programs. Thanks bro🎉🎉❤
first not frist !!!!!!!!
How great code I really love this channel, keep up the good work
What a great content here. Thank you Bro 🤩
Wow ha so much fun with this :) Thanks Bro!!
### 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
print(f"{hours:02}:{minutes:02}:{seconds:02}")
time.sleep(1)
countdown_time -= 1
print("Time's up!")
wow...that's so cool
bro is now my new teacher
great video
Is they a way to display the counting without looping every number but stationarily changing the digits
LITTLE REMINDER IF YOU WANT TO MAKE THE TIMER A SINGLE LINE
import os
and put the code “os.system(“cls”) under the for loop
Amazing!
love your videos :)
You'r amazing at codding
How did you even Learn it
Great. I really enjoyed this countdown. I would like to know how to make the mouse click on a button on a website when the count is finished. Thanks.
import time
#if anyone is too lazy to convert minutes into seconds(like me)
num=int(input("Enter time in minutes to convert into seconds::: "))
print(f"Time is {num*60}")
time_1=int(input("Enter time in seconds::: "))
for i in range(time_1, 0, -1):
seconds = i % 60
minutes = int(i / 60) % 60
hours = int(i / 3600)
print(f"{hours:02}:{minutes:02}:{seconds:02}")
time.sleep(1)
print("Time's up!")
i swear i'l be back one day and I will say "I got it, I fcking got it"
I love your vids bro
At the starting of the video:-
missing_this_a_lot = {1:’Sitback’, 2:’Relax’, 3:’Enjoy the show’}
print(“”)
You are the real BRO!
amazing
Thank you bro
Bro! Thank you
THANKS!
Thanks
import time
def timer():
while True:
try:
inputSec = int(input("How many seconds do we have left? "))
if inputSec 0:
Sec = inputSec % 60
Min = int(inputSec / 60) % 60
Hrs = int(inputSec / 3600)
print(f"{Hrs:02}:{Min:02}:{Sec:02}")
inputSec -= 1
time.sleep(1)
print("It's joever.")
print()
while True:
timer()
Can you please give some explanation on the x %60 thing bro
X 2 i don't understand why we must use the modulus (%) 60.
Maybe because we have a maximum of 60 seconds (?)
because of the %60 function (the remainder of the division by 60), you can divide seconds to minutes, so if, for example, you entered 65 seconds, then because of the 60% thing the timer will first count 5 seconds, and then count 60 seconds.
It took me a while to understand.
thanks bro
Thank you bro code
understandable
if you could tell me the purpose of seconds = x % 60, that'd be great
can somone explaine why did he do:
seconds = x%60
i didnt understand how does this thing work
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)
days = int(x / 86400) % 24
print(f"{days:02}:{hours:02}:{minutes:02}:{seconds:02}")
time.sleep(1)
print("TIME'S UP!")
import time
while True:
user_input = input("Enter time in seconds: ")
if user_input.isdigit():
break
else:
print("That's not an integer! Please enter an integer.")
for i in range(int(user_input), -1, -1):
hours = i // 3600
minutes = (i % 3600) // 60
seconds = i % 60
print(f"{hours:02d} hours:{minutes:02d} minutes:{seconds:02d} seconds")
time.sleep(1)
print("Time's up!")
The best channel on youtube. long story short. you saved me from commiting suicide
bro we can add sound using winsound module
import time
import winsound
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}")
time.sleep(1)
print("TIME'S UP!")
winsound.Beep(1001,3000 )
Instead of int(x/60) you should use x//60
i love you
man i am suck at meth, I'd never thought that one day I need to cook not basic or not even low Quality meth, but real meth, I think I need to start cooking meth now like Heisenberg
Why you use x % 60 i didn't get ...like why you use ... everyone in the comments shows that they got it but i don't..i am begginers to python and to what i read in the comments i tink to programming
First you should know what the difference is between % and /. If X=6 then
X%4 = 2 but
X/4 = 1.
I hope it will help you.🎉
You’re amazing❤
what if we need to clear console before printing again plzzz help
Actually the countdown isn’t that accurate because execution of each line takes time, so it will be longer than 1 second each loop.
Time it and let us know
I have a doubt, all the time I code it says (process finished with exit code 0)
can please help me fix it
What to do when we put 3
which version of python do you use
@Bro Code i have question why you add {time:02}, what is 02 mean ?? can you explain for me pls?
Hi! I don't know if you still need the explanation but you can find it in this previous tutorial: ruclips.net/video/FrvBwdAU2dQ/видео.html&pp=iAQB
can you add pause and resume to this coundoun?
Is there a way to reset console on each iteration of the cycle? to make it look like a real timer
yes, but that might be a topic for another video... it's complicated
How do I impliment a timer running in the background of my program?
Resulting users only able to provide inputs until the timer runs out.
You need the timer tu run in another thread, search for python threading
am i lying to myself if i write the code while watching the tutorial? am i learning from it?
nah, thats how i learn and its working. if you dont get it at first just rewatch
How do I I make milliseconds on my countdown timer?
Bro you are the best 💕!! Can you give a heart to my comment
ok
getting a little harder now
What if the user doesn't wanna convert the timer to seconds.???
Edit: Nvm i figured it out
yoo can you share please?
i still need your code
is it possible to have the time printed only once and not every second?
use end="
" at your print statement
can anyone explain wy we use modulus 60
why did u used 02?
Slowly became my most watched channel lol
does anyone know what editor he's using?
Pycharm
i am stupid asfk
hi
hi
He Started Reuploading His Old Videos And Making More Sense