|
| 1 | +# import modules like 'os' and 'time' |
| 2 | +import os |
| 3 | +import time |
| 4 | + |
| 5 | +os.system('clear') |
| 6 | + |
| 7 | +# using ctime() to show present time |
| 8 | +times = time.ctime() |
| 9 | +print("\nCurrent Time: ",times) |
| 10 | + |
| 11 | +print("\n Welcome to CountdownTimer!\n\n Let's set up the countdown timer...\n") |
| 12 | + |
| 13 | +# User input for the timer |
| 14 | +hours = int(input(" How many hours? ")) |
| 15 | +minutes = int(input(" How many minutes? ")) |
| 16 | +seconds = int(input(" How many seconds? ")) |
| 17 | + |
| 18 | +# To display message when the given value is not a number |
| 19 | +if hours or minutes or seconds == "": |
| 20 | + print("\n Invalid entry. You must enter a number.") |
| 21 | + |
| 22 | +# Conversion of hours amd minutes into seconds |
| 23 | +hrsToSec = (hours * 60) * 60 |
| 24 | +mnsToSec = (minutes * 60) |
| 25 | +seconds = seconds |
| 26 | + |
| 27 | +seconds = hrsToSec + mnsToSec + seconds |
| 28 | +print("\n Timer has been set for "+str(seconds) + " seconds.") |
| 29 | + |
| 30 | +# Loop for displaying the timer |
| 31 | + |
| 32 | +for i in range(seconds, -1, -1): |
| 33 | + displayHours = int(seconds / 3600) |
| 34 | + displayMinutes = int(seconds / 60) |
| 35 | + if displayMinutes >= 60: |
| 36 | + displayMinutes = displayMinutes - (displayHours * 60) |
| 37 | + else: |
| 38 | + displayMinutes = displayMinutes |
| 39 | + displaySeconds = int(seconds % 60) |
| 40 | + print("\n Your time remaining is: {}:{}:{}".format(str(displayHours).zfill(2), str(displayMinutes).zfill(2), str(displaySeconds).zfill(2))) |
| 41 | + seconds -= 1 |
| 42 | + time.sleep(1) # delays in the excution of a program for 1 second |
| 43 | + |
| 44 | +print("\n Time is over.") |
| 45 | + |
| 46 | + |
0 commit comments