Seems like there should be a simple solution using the time module or something, but I’ve tried a few things and nothing seems to work. I need something like this to work:
hungry = True
if line.find ('feeds'):
#hungry = False for 60 seconds, then hungry is true again
Anyone have a solution for this?
edit: As for what I’ve tried, I’ve tried this bit of code:
if hungry==True:
print('yum! not hungry for 20 seconds')
hungry = False
i = 20
while(i>0):
i-=1
time.sleep(1)
if(i==0):
hungry = True
But that doesn’t work because the program just pauses until hungry is True again, and hungry being false while the program sleeps won’t help; it should be false for a certain amount of time while the rest of the program works
edit: It looks like this won’t be possible without threading. I’ll have to either find a new solution, or learn to use threading. Thanks for all the help anyways, I seriously appreciate it!
You could encapsulate the behaviour you want in a
TimedValueclass, but that might be overkill here– I’d probably just do something likeand then use
hunger()whenever I wanted the value instead ofhungry. That way, the code doesn’t block, and we can continue doing work, buthunger()will give us the right state. E.g.produces