0

I have this code, but why when the time reach 0:30 nothing happens, where is my error?:

import datetime

def a():

   timing = [0, 30] #hours, #minutes
   while True:
       now = datetime.datetime.now()
       datas = [now.hour, now.minute]    
       if datas == timing:
            a.x = 5
        
def b():
    a()
    if "he" == "he":
        print(2)
    
         if a.x == 5:
             print("VER")
    
b()        
14
  • Are you defining "a.x" within a()? Because you can only do that within classes, not functions. Commented Nov 8, 2021 at 23:46
  • 2
    You never break out of the while loop, so a() never returns. Commented Nov 8, 2021 at 23:47
  • @GIOVANNIQUINONESVALDEZ Functions are class instance, so you can create attributes on them. Commented Nov 8, 2021 at 23:48
  • @Barmar You are right1 Commented Nov 8, 2021 at 23:49
  • Add break or return to the if block in a(). Commented Nov 8, 2021 at 23:49

1 Answer 1

1

I believe because you don't leave the while loop. You should add a break to if condition. It means if a.x is set, then program should leave the function a and get back to function b.

def a():

   timing = [18, 54] #hours, #minutes
   while True:
       now = datetime.datetime.now()
       datas = [now.hour, now.minute]    
       if datas == timing:
            a.x = 5
            break
        
def b():
    a()
    if "he" == "he":
        print(2)
    
        if a.x == 5:
            print("VER")
    
b()
Sign up to request clarification or add additional context in comments.

5 Comments

he said he tried this and it didn't work.
Are you sure you change the values in timing based on the current time? I mean if you run the code now, nothing happens till we get datas == timing. So for testing, you should change the timing!
See his last comment. It did work, he just forgot to restart after editing.
@amir It works thanks, and what if i want to avoid the "break",is there a way to run the functions without the "break"?
@TreesJin you can use return instead of break

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.