[英]Why doesn't this python function get executed twice in a while loop?
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.我正在尝试制作一个简单的(对我来说不是真的)早安脚本,它在 while 循环中等待几秒钟,触发 function,然后再次等待再次触发。
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.安装调度程序没有用,做 threaded.Timer 没有用,不知道如何使用 deltatime,因为它看起来比我需要做的更复杂。 Basically, this is way harder than it needs to be, with not a lot of information on why the function doesnt fire twice.基本上,这比它需要的要难得多,没有太多关于为什么 function 不触发两次的信息。
Here is the while loop:这是 while 循环:
while days_run < 325:
print("Program Started")
time.sleep(10)
goodmorning()
here is the function:这是 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.我在 while 循环上做了一个断点,它进入睡眠定时器,然后执行 function,然后无限地停止。
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?我不明白这怎么行不通,为什么它必须比这更复杂,我可能做错了什么?
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.现在你的 while 循环不依赖于days_run<325:
因为你没有在运行的 while 循环中修改你的days_run
变量。
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.此代码将运行 4 次while
循环并在每次Program Started
打印后sleep 10sec
。
Output: Output:
Program Started
Morning Working
Program Started
Morning Working
Program Started
Morning Working
Program Started
Morning Working
Here you go, next time be more specific给你go,下次具体点
days_run = 0;
import time
def goodmorning():
print("Morning Working")
while days_run < 1:
print("Program Started")
time.sleep(1)
goodmorning()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.