So i made a script and it works fine but it could be better.

The code will be written below, the purpose of the script is to make a beep sound when the timer runs out (that works) but i also want to show on the screen how many hours minutes and seconds it takes (and possibly days, weeks, months, years)

If you run this script it will make a beep in two hours but what it will show to the user is this:
2 hours, 120 minutes and 7199 seconds
1 hours, 119 minutes and 7198 seconds

So i want to make the script smarter and make it really work as a countdown clock. And make it say this:
2 hours, 0 minutes and 0 seconds
1 hours, 59 minutes and 59 seconds
etc...

like a real countdown clock, what my script just does is show the total time instead of calculating how many minutes of the hour are left and how many seconds of a minute are left.

import os,time,winsound
    hours=2
    minutes=0
    sec=0

    counter=hours*3600+minutes*60+sec
    mins=int(counter/60)
    hr=int(mins/60)

    while counter > 0:
        counter-=1
        print('%i hours, ' %hr + '%i minutes ' %mins + 'and %s seconds' %counter)
        mins=int(counter/60)
        hr=int(mins/60)
        time.sleep(1)


    Freq = 1000 # Set Frequency To 2500 Hertz
    Dur = 500 # Set Duration To 1000 ms == 1 second
    winsound.Beep(Freq,Dur)

You can try

hr, sc = divmod(counter, 3600)
mn, sc = divmod(sc, 60)
msg = '%d hours, %d minutes and %d seconds' % (hr, mn, sc)
print(msg)
commented: neat +14

Thanks a million works like a charm, changed the script into:

import os,time,winsound
hours=2
minutes=1
sec=0

counter=hours*3600+minutes*60+sec
mins=int(counter/60)
hr=int(mins/60)

while counter > 0:
    counter-=1
    hr, sc = divmod(counter, 3600)
    mn, sc = divmod(sc, 60)
    print ('%d hours, %d minutes and %d seconds' % (hr, mn, sc))
    mins=int(counter/60)
    hr=int(mins/60)
    time.sleep(1)


Freq = 1000 # Set Frequency To 2500 Hertz
Dur = 500 # Set Duration To 1000 ms == 1 second
winsound.Beep(Freq,Dur)
import time, os

def main():
    hours = float(input("Input hours : "))
    minutes = float(input("Input minutes : "))
    sec = float(input("Input seconds : "))
    os.system('CLS')

    counter=hours*3600+minutes*60+sec
    mins=int(counter/60)
    hr=int(mins/60)

    while counter > 0:
        os.system('CLS')
        counter-=1
        hr, sc = divmod(counter, 3600)
        mn, sc = divmod(sc, 60)
        print ("########################################")
        print("")
        print ("  %d hours, %d minutes and %d seconds" % (hr, mn, sc))
        print("")
        print ("########################################")
        mins=int(counter/60)
        hr=int(mins/60)
        time.sleep(1)
    else:
        print("Times up!")

main()

This allows user input to put in the time you want to count down from!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.