Search code examples
pythontimemoduletiming

Trouble with a Python Countdown Program


I have recently finished a course in Python about a month ago. I am continuing expanding my knowledge on Python by creating programs.

The code below is a program for a Python countdown program. What the program does is that it asks the user to input a count of seconds, minutes, and hours.

Anyways, I noticed 2 Bugs that occur in the program.

First Bug: If we enter an hour and second count (and no minute count), the program would count the remaining time for that certain hour, but it would not subtract the hour and set the minute back to 59. Instead, it would print the minute as a negative number.

Second Bug: If we enter an hour, second, and minute count, the program would count the reamaining time. But when the program reaches the very last minute (i.e. 01:00:59), it would skip that minute and go on to the next one (i.e. 00:59:59).

About the 2nd Bug: Suppose I enter 1 Hour, 1 Minute, 5 Seconds. It would count down the 5 Seconds. The computer would then print 1:00:00. Instead of counting down the seconds for that certain minute. It would skip to 0:59:59. Hope that helps It would be fantastic if I could receive some assistance with fixing these two bugs, and maybe a way to differently format the program.

Thanks for reading and I am looking forward to your answer, -Anonymous

import time
time_count = 0
    second = int(raw_input("Seconds:"))
    count_minute = int(raw_input("Minutes:"))
count_hour = int(raw_input("Hours:"))

    time_count += second
time_count += count_minute * 60
time_count += count_hour * 3600

    def clean():    
    global second
    global count_minute
    global count_hour
    print_second = str(second).zfill(2)
    print_minute = str(count_minute).zfill(2)
    print_hour = str(count_hour).zfill(2)
    print "%s:%s:%s" % (print_hour, print_minute, print_second)
time.sleep(1)
clean()
time.sleep(1)
for i in range(1, time_count + 1)[::-1]:
    if second == 0 and count_minute == 0 and count_hour == 0:
        clean()
        break 
    elif second != 0:
        second -= 1
    elif second == 0:
        count_minute -= 1
        second = 59
        if count_minute == 0 and count_hour > 0:
            clean()
            count_hour -= 1
            count_minute = 59
    clean()
    time.sleep(1)
print """
Timer Finished.
"""

Solution

  • Some problems in your code are, the unnecessary use of globals, direct typechecking, etc. Also if you use higher level constructions(like timedelta objects) the probability of your code being bug free is higher. This is better:

    from datetime import timedelta
    from time import sleep
    
    while True:
        try:
            hours, minutes, seconds = input('Enter hours, minutes, seconds (with a comma in between): ')
        except (ValueError, TypeError):    # other errors
            print("Error 1, please ...")
            continue
        except NameError:
            print("Error 2")
            continue
        else:
            print("All good")
            break
    
    total = timedelta(hours=hours, minutes=minutes, seconds=seconds)
    for passed_seconds in range(int(total.total_seconds())):
        print total - timedelta(seconds=passed_seconds)
        sleep(1)