Home > Enterprise >  Why doesn't this python function get executed twice in a while loop?
Why doesn't this python function get executed twice in a while loop?

Time:01-20

I'm trying to make a simple (not really, to me) good morning script that waits a couple of seconds in the while loop, fires a function, then waits again to fire again.

installing scheduler didn't work, doing threaded.Timer didn't work, no idea how to use deltatime because its look more complicated than what I need to do. Basically, this is way harder than it needs to be, with not a lot of information on why the function doesnt fire twice.

Here is the while loop:

while days_run < 325:
    print("Program Started")
    time.sleep(10)
    goodmorning()

here is the function:

def goodmorning():
    print("Morning Working")

It executes the first time then never again. I do a breakpoint on the while loop and it goes to the sleep timer, then executes the function, then stalls infinitely.

I don't understand how this doesn't work, why does it have to be more complex than this, what could I possibly have done wrong?

CodePudding user response:

Right now your while loop is not dependent on days_run<325: because you are not modifying your days_run variable in a running while loop.

you can do like..

import time
def goodmorning():
    print("Morning Working")

days_run=0
while days_run < 4:
    print("Program Started")
    days_run =1    #Modification of days_run
    time.sleep(10)
    goodmorning()  

This code will run 4 times while loop and sleep 10sec after every time Program Started printed.

Output:

Program Started
Morning Working
Program Started
Morning Working
Program Started
Morning Working
Program Started
Morning Working 

CodePudding user response:

Here you go, next time be more specific

days_run = 0;
import time


def goodmorning():
    print("Morning Working")

while days_run < 1:
    print("Program Started")
    time.sleep(1)
    goodmorning()
  • Related