Home > Software engineering >  Timer script wont revert back to the starting point when the function has been returned?
Timer script wont revert back to the starting point when the function has been returned?

Time:10-17

In this Python script(Visual studio) I am attempting to make a timer that counts seconds, resets at the 60 second mark, adds a minute and counts the seconds again. However, Instead of stopping at 60, it keeps going on, 61,62,63 etc. this only happens after the 1st minute is completed and does not happen on the first minute.

def ClockTimer():
import time
number = 0
minutes = 0
while True:
    
    time.sleep(1)
    print(number   1, "seconds")
    number  = 1  

    if number == 60:
        
        print("When hits 60")
        number = 0 
        minutes  = 1
        while True:
            print( minutes, "minutes", number   1, "seconds",)
            number  = 1
            time.sleep(1)
        return ClockTimer
print(ClockTimer())

I have some examples I took on the recording software gyazo. Hitting the first minute. Hitting second minute, onwards.

If anyone can help me I would really appreciate it. The reason why the question might be simple is because I just started python and I thought this would be a good started project. Thank you in advance.

CodePudding user response:

Your problem is the second while loop, when you hit 60 you stay in this inner loop forever:

while True:
    print( minutes, "minutes", number   1, "seconds",)
    number  = 1
    time.sleep(1)

I don't see why you would return the function itself and print it, you print the results within the function already. Here's a slightly modified version that runs correctly, I let it return after reaching 10 minutes and commented out the sleep so I wouldn't have to wait 10 minutes for it to complete running :)

def ClockTimer():
    import time
    number = 0
    minutes = 0
    while minutes < 10:    
        #time.sleep(1)
        if minutes > 0:
            print( minutes, "minutes", number   1, "seconds",)
        else:
            print(number   1, "seconds")
        number  = 1          
        if number == 60:          
            print("When hits 60")
            number = 0 
            minutes  = 1

ClockTimer()
  • Related