Countdown timer program in Python ⌛

Поділитися
Вставка
  • Опубліковано 27 лис 2024

КОМЕНТАРІ • 132

  • @BroCodez
    @BroCodez  2 роки тому +62

    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!")

    • @shiningstar9403
      @shiningstar9403 2 роки тому +1

      can you make this same video but using WHILE LOOP ??

    • @BroCodez
      @BroCodez  2 роки тому +14

      @@shiningstar9403 yes
      while my_time > 0:
      you'll need to manually decrement my_time by one during each iteration tho

    • @KJ7JHN
      @KJ7JHN 2 роки тому

      How is this converted into a background thread Bro?

    • @letsspeakup308
      @letsspeakup308 Рік тому

      @@BroCodez hi can you make it in java with gui

    • @kanggsaga
      @kanggsaga 8 місяців тому

      Hey Bro Code, what do the “:02” between minute and houre and second do. Why we to add the :02

  • @tigerex777
    @tigerex777 Рік тому +30

    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.

    • @icyy-0v0-
      @icyy-0v0- 9 місяців тому +1

      This helped so much thank you

    • @knitheesh7559
      @knitheesh7559 7 місяців тому +1

      Yeah I also stucked there only 😑but your comment helps very much to under stand 🎉

    • @AdityaVerma-vy2en
      @AdityaVerma-vy2en 4 місяці тому +1

      damn what a helpful explanation. Brocode should pin your comment

    • @hepp00
      @hepp00 3 місяці тому

      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.

  • @hosseinrezaei9844
    @hosseinrezaei9844 4 місяці тому +2

    ### 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!")

  • @ianboyer2224
    @ianboyer2224 Рік тому +21

    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

    • @MoreBilaINoFilter
      @MoreBilaINoFilter Рік тому

      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

    • @stefantrajkovic9392
      @stefantrajkovic9392 5 місяців тому

      ​@@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)

  • @Homnuangirl
    @Homnuangirl 10 місяців тому +4

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

  • @ru_uwu
    @ru_uwu Рік тому +9

    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

    • @noliyeb
      @noliyeb Рік тому +1

      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 ???

    • @immersivelandscape2472
      @immersivelandscape2472 Рік тому

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

    • @NativeAspect
      @NativeAspect 11 місяців тому +1

      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!")

  • @kunalkamthe8039
    @kunalkamthe8039 2 роки тому +2

    This Channel deserve 10 million subscribers

  • @myeducationvlogs8483
    @myeducationvlogs8483 2 роки тому +8

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

  • @thassadub2749
    @thassadub2749 Рік тому +1

    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!

  • @markotomasevic8028
    @markotomasevic8028 9 місяців тому

    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.

  • @KunjMandaviya
    @KunjMandaviya Рік тому +2

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

    • @omairtech6711
      @omairtech6711 Рік тому

      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.

    • @noliyeb
      @noliyeb Рік тому

      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 ???

  • @ahura9775
    @ahura9775 6 місяців тому +1

    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

  • @walidkhaled2387
    @walidkhaled2387 Рік тому

    that was a great exercise sir thank you so much

  • @soupdog6767
    @soupdog6767 Рік тому

    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!")

  • @Leerata
    @Leerata Рік тому +2

    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

    • @efeley44
      @efeley44 Рік тому +1

      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. ')

    • @noliyeb
      @noliyeb Рік тому

      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 ???

  • @the_memes_explorer
    @the_memes_explorer Рік тому +1

    It worked🎉. My frist programs. Thanks bro🎉🎉❤

  • @winchestermascarenas1527
    @winchestermascarenas1527 Рік тому +4

    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.

    • @sbvforworld4290
      @sbvforworld4290 Рік тому +1

      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

    • @noliyeb
      @noliyeb Рік тому

      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 ???

    • @hepp00
      @hepp00 3 місяці тому

      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

  • @extendedsomeone
    @extendedsomeone 14 днів тому

    #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)
    print(f"{hours:02}:{minutes:02}:{seconds:02}")
    time.sleep(1)
    print("TIME'S UP!")

  • @user-anonymous012
    @user-anonymous012 7 місяців тому +1

    marvelous content.

  • @-SakuraNoHana-
    @-SakuraNoHana- 6 місяців тому

    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!")

  • @sufiyanshaikh5779
    @sufiyanshaikh5779 2 роки тому +2

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

  • @tasogare.1017org
    @tasogare.1017org 2 роки тому

    bro is now my new teacher

  • @jeffery5672
    @jeffery5672 Рік тому

    great video

  • @GabiXwaver
    @GabiXwaver Рік тому

    can somone explaine why did he do:
    seconds = x%60
    i didnt understand how does this thing work

  • @EfemeRoyal
    @EfemeRoyal Рік тому

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

  • @farhanishraq5812
    @farhanishraq5812 Рік тому

    amazing

  • @kapibara2440
    @kapibara2440 Рік тому

    What a great content here. Thank you Bro 🤩

  • @endtropi
    @endtropi Рік тому +1

    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

    • @POWER-WAIFU
      @POWER-WAIFU Рік тому +3

      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

    • @shahromehaider7259
      @shahromehaider7259 Рік тому

      chat gpt used in good way hahahahahah
      @@POWER-WAIFU

  • @improvizr7380
    @improvizr7380 2 роки тому +2

    Bro thank u soo much u saved my life literally ❤

  • @reincarnationofthelightking
    @reincarnationofthelightking 2 місяці тому

    I got it, I fcking got it, i needed 5 minutes to comprehend all magic, thank you bro

  • @zaydarendse2812
    @zaydarendse2812 Рік тому

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

  • @Ravi.Kumar-
    @Ravi.Kumar- 2 роки тому

    At the starting of the video:-
    missing_this_a_lot = {1:’Sitback’, 2:’Relax’, 3:’Enjoy the show’}
    print(“”)

  • @Devaanshikhanna
    @Devaanshikhanna Рік тому

    You'r amazing at codding

  • @MD0407
    @MD0407 7 місяців тому

    wow...that's so cool

  • @TrishalMishra
    @TrishalMishra 25 днів тому

    Thank you 😊 😊

  • @bezoekers
    @bezoekers Рік тому

    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()

  • @TIPSFIN
    @TIPSFIN Рік тому

    The best channel on youtube. long story short. you saved me from commiting suicide

  • @Jcongothenpc
    @Jcongothenpc 10 місяців тому

    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

  • @MostaphaQamar
    @MostaphaQamar Рік тому

    Thanks

  • @lovelyliaella2159
    @lovelyliaella2159 2 роки тому

    Amazing!

  • @driarol
    @driarol Рік тому

    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.

  • @dinkondbd
    @dinkondbd 2 роки тому

    I love your vids bro

  • @LuqmanAdelani
    @LuqmanAdelani Рік тому

    You are the real BRO!

  • @reincarnationofthelightking
    @reincarnationofthelightking 2 місяці тому

    i swear i'l be back one day and I will say "I got it, I fcking got it"

  • @AllenDende
    @AllenDende Рік тому

    understandable

  • @filimontesfaye927
    @filimontesfaye927 2 роки тому +3

    Can you please give some explanation on the x %60 thing bro

    • @mateo1481
      @mateo1481 2 роки тому

      X 2 i don't understand why we must use the modulus (%) 60.
      Maybe because we have a maximum of 60 seconds (?)

    • @timofeyp4965
      @timofeyp4965 Рік тому +7

      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.

  • @animarvellous2253
    @animarvellous2253 Рік тому +1

    Is they a way to display the counting without looping every number but stationarily changing the digits

  • @Houman.hafezz
    @Houman.hafezz 2 роки тому

    love your videos :)

  • @ehudkotegaro
    @ehudkotegaro Рік тому

    Instead of int(x/60) you should use x//60

  • @emmanuelgibson6657
    @emmanuelgibson6657 Рік тому

    Thank you bro

  • @gamePlays9699
    @gamePlays9699 Рік тому

    THANKS!

  • @texman1166
    @texman1166 4 місяці тому

    Thank you. New Subscriber.

  • @hil6517
    @hil6517 2 роки тому

    thanks bro

  • @ahura9775
    @ahura9775 6 місяців тому

    if you could tell me the purpose of seconds = x % 60, that'd be great

  • @awilson144
    @awilson144 Рік тому

    Bro! Thank you

  • @Cdictator
    @Cdictator 9 місяців тому

    Actually the countdown isn’t that accurate because execution of each line takes time, so it will be longer than 1 second each loop.

    • @AnglerBirb
      @AnglerBirb 4 місяці тому

      Time it and let us know

  • @snahhhhhhh
    @snahhhhhhh Рік тому

    Thank you bro code

  • @nikilnikki18
    @nikilnikki18 2 роки тому +1

    Bro you are the best 💕!! Can you give a heart to my comment

  • @Game_guy555
    @Game_guy555 11 місяців тому

    which version of python do you use

  • @RKmemz
    @RKmemz 11 місяців тому

    what if we need to clear console before printing again plzzz help

  • @ShikamaruAppiah-Kubi
    @ShikamaruAppiah-Kubi Рік тому

    i love you

  • @andygambo1684
    @andygambo1684 Місяць тому

    guys i tried putting in 3675 the minutes hands started reading from 61 i mean 01:61:14 which is wrong any help

  • @samahghaith5831
    @samahghaith5831 Рік тому +1

    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

    • @bibekkoirala5294
      @bibekkoirala5294 10 місяців тому

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

  • @ronaldoromerovergel8373
    @ronaldoromerovergel8373 11 місяців тому

    am i lying to myself if i write the code while watching the tutorial? am i learning from it?

    • @fistibro7885
      @fistibro7885 10 місяців тому +1

      nah, thats how i learn and its working. if you dont get it at first just rewatch

  • @efti56209
    @efti56209 Рік тому

    I have a doubt, all the time I code it says (process finished with exit code 0)
    can please help me fix it

  • @lukesukulelechannel
    @lukesukulelechannel 2 роки тому

    You’re amazing❤

  • @Regr310
    @Regr310 Рік тому

    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.

    • @RobDom01
      @RobDom01 Рік тому +1

      You need the timer tu run in another thread, search for python threading

  • @reincarnationofthelightking
    @reincarnationofthelightking 2 місяці тому

    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

  • @EsonaEsonder
    @EsonaEsonder Рік тому

    @Bro Code i have question why you add {time:02}, what is 02 mean ?? can you explain for me pls?

    • @Mel-Night
      @Mel-Night Рік тому +2

      Hi! I don't know if you still need the explanation but you can find it in this previous tutorial: ua-cam.com/video/FrvBwdAU2dQ/v-deo.html&pp=iAQB

  • @NoobPerson-xp7nn
    @NoobPerson-xp7nn 3 місяці тому

    What if the user doesn't wanna convert the timer to seconds.???
    Edit: Nvm i figured it out

  • @idosiris
    @idosiris Рік тому

    can you add pause and resume to this coundoun?

  • @BingableRicha
    @BingableRicha 10 місяців тому

    What to do when we put 3

  • @СергейВойтенко-г9з

    Is there a way to reset console on each iteration of the cycle? to make it look like a real timer

    • @BroCodez
      @BroCodez  2 роки тому +1

      yes, but that might be a topic for another video... it's complicated

  • @Ted_likes_Rice
    @Ted_likes_Rice Рік тому

    How do I I make milliseconds on my countdown timer?

  • @lourencocastro8799
    @lourencocastro8799 Рік тому

    is it possible to have the time printed only once and not every second?

    • @ru_uwu
      @ru_uwu Рік тому

      use end="
      " at your print statement

  • @envy_coppr2146
    @envy_coppr2146 2 роки тому

    Slowly became my most watched channel lol

  • @jatinbhoir6988
    @jatinbhoir6988 7 місяців тому

    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 )

  • @aakrit4421
    @aakrit4421 10 місяців тому

    why did u used 02?

  • @ben_ruby
    @ben_ruby Рік тому

    does anyone know what editor he's using?

  • @brianworld3887
    @brianworld3887 Рік тому

    getting a little harder now

  • @noro5880
    @noro5880 Рік тому

    can anyone explain wy we use modulus 60

  • @originalni_popisovac
    @originalni_popisovac 2 роки тому

    hi

  • @sh4d0wslimer
    @sh4d0wslimer День тому

    I understood nothing

  • @IlIIllIlIlIIlIlIlIlIIl
    @IlIIllIlIlIIlIlIlIlIIl 7 місяців тому

    i am stupid asfk

  • @furqoncalas
    @furqoncalas 22 дні тому

    pada bahasa apaan sihh ora bisa indonesia yaa lulpada

  • @scpf2360
    @scpf2360 2 роки тому

    He Started Reuploading His Old Videos And Making More Sense