Python : Basic countdown timer & function() > int()

前端 未结 3 749
温柔的废话
温柔的废话 2021-01-26 03:04

I\'m trying to ucreate a timer function that runs in the background of my code and make it so I can use/check the time. What I mean by use/check, I\'m trying to make it so I can

3条回答
  •  故里飘歌
    2021-01-26 03:42

    The problem with your code is that when you run hall(), Python first executes the whole of timer() (i.e. the whole for loop), and then moves on with the rest of the code (it can only do one thing at a time). Thus, by the time it reaches the while loop in hall(), timer is already 0.

    So, you're going to have to do something about that timer so that it counts down once, and then it moves on to the do something part.

    Something that you can do is this:

    def hall():
        for a in range(0, 15):
            print(15 - a)
            # do something
            time.sleep(1)
    

    This should work just fine (if you're only executing hall 15 times), and condenses your code to just one function.

提交回复
热议问题