Countdown timer program in Python ⌛

Поділитися
Вставка
  • Опубліковано 24 жов 2022
  • #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!")

КОМЕНТАРІ • 117

  • @BroCodez
    @BroCodez  Рік тому +56

    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 Рік тому +1

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

    • @BroCodez
      @BroCodez  Рік тому +12

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

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

      How is this converted into a background thread Bro?

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

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

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

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

  • @xadar.
    @xadar. Рік тому +60

    Just love this human being.

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

      Please do not bomb him laden's son

    • @benjaminjohnpabroquez8012
      @benjaminjohnpabroquez8012 Рік тому +5

      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

    • @Kmsyt
      @Kmsyt 10 днів тому

      We all do!

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

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

  • @thassadub2749
    @thassadub2749 8 місяців тому +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 5 місяців тому

    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.

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

    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 11 днів тому

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

  • @tigerex777
    @tigerex777 8 місяців тому +7

    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- 4 місяці тому

      This helped so much thank you

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

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

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

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

  • @myeducationvlogs8483
    @myeducationvlogs8483 Рік тому +8

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

  • @user-qv6nt9dj8e
    @user-qv6nt9dj8e 10 місяців тому

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

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

    that was a great exercise sir thank you so much

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

    This Channel deserve 10 million subscribers

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

    Many Many Thanks!! You are being too helpful!

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

    Bro thank u soo much u saved my life literally ❤

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

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

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

    What a great content here. Thank you Bro 🤩

  • @ru_uwu
    @ru_uwu 11 місяців тому +7

    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 10 місяців тому +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 ???

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

      Many thanks, that is a good hint

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

      @@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 6 місяців тому +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!")

  • @user-anonymous012
    @user-anonymous012 2 місяці тому

    marvelous content.

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

    Amazing!

  • @Houman.hafezz
    @Houman.hafezz Рік тому

    love your videos :)

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

  • @tasogare.1017org
    @tasogare.1017org Рік тому

    bro is now my new teacher

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

    I love your vids bro

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

    great video

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

    wow...that's so cool

  • @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 10 місяців тому

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

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

    • @user-zb4fo2os6v
      @user-zb4fo2os6v 9 місяців тому +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 8 місяців тому

      chat gpt used in good way hahahahahah
      @@user-zb4fo2os6v

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

    THANKS!

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

    You are the real BRO!

  • @pemarathnaw.a.-xr4xv
    @pemarathnaw.a.-xr4xv Рік тому

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

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

    amazing

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

    Bro! Thank you

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

    Thank you bro

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

    thanks bro

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

    Thanks

  • @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 10 місяців тому

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

  • @user-zq7db7rf6g
    @user-zq7db7rf6g 7 місяців тому

    understandable

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

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

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

    Thank you bro code

  • @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 10 місяців тому

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

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

    You'r amazing at codding

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

    You’re amazing❤

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

    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

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

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

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

    i love you

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

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

  • @Ravi.Kumar-
    @Ravi.Kumar- Рік тому

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

  • @filimontesfaye927
    @filimontesfaye927 Рік тому +3

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

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

      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.

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

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

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

    can you add pause and resume to this coundoun?

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

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

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

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

    which version of python do you use

  • @user-lp9hh3wq9e
    @user-lp9hh3wq9e Рік тому

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

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

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

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

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

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

    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

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

    How do I I make milliseconds on my countdown timer?

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

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

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

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

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

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

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

    What to do when we put 3

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

    can anyone explain wy we use modulus 60

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

    why did u used 02?

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

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

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

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

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

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

      use end="
      " at your print statement

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

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

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

    Slowly became my most watched channel lol

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

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

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

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

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

    getting a little harder now

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

    does anyone know what editor he's using?

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

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

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

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

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

    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 )

  • @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 5 місяців тому

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

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

    hi

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

    i am stupid asfk

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

    He Started Reuploading His Old Videos And Making More Sense