Search code examples
pythonpython-3.xloopsnumberscountdowntimer

The loop i tried to make won't loop correctly


I tried to make a timer by making a function, that should keep on running until it hit 0. I thought I could get it to loop, but I think it ended up skipping the first part of else
"(int(time) - int(1))" and just repeating the second numeral it got after - 1. What i want it to do: Take the whole function and run it through until it reaches 0. What it does: takes time -1 and keeps printing that until it reaches maximum recursion depth.

import time as tm

def Timer(time):
    if time == '0':
        print("done")
        tm.sleep(3)
    else:
        print(int(time) - int(1))
        Timer(time)
Timer(time)

Solution

  • I made some small alterations on your code. Though it's very similar, I fixed the missing parameter and removed all the variable type convertions, since it didn't seem necessarry. Here's what I created (of course, you may have to adapt the function calling and probably remove my input):

    import time as tm
    
    def Timer(time):
    
        while (time>0):
    
            tm.sleep(1)
            time -= 1
            print(time)
    
        print("done")
        tm.sleep(3)
    
    time=int(input('Choose your time value: '))
    Timer(time)