mercredi 29 juillet 2020

Why is the if statement not triggering when the target time is hit? [duplicate]

I cannot seem to figure out why the if statement in the call method will not trigger. When printing self.minutes(self.time) and target_time, they show the same values (before running the script I set target_time to the current time + 1 minute, so it is possible to trigger), but the if statement is not getting triggered.

from datetime import datetime
import time

class Time:
    def __init__(self):
        super().__init__()
        self.time = datetime.utcnow() 
    def update_time(self):
        self.time = datetime.utcnow() 
    def minutes(self, time):
        return str(time)[11:16] # Turn self.time datetime object into string as hh:mm format

class A(Time):
    def __init__(self):
        super().__init__()

    def call(self):
        target_time = "18:56" # Time that will trigger the if statement. Should be set to a time ahead of the current time.
        print(self.minutes(self.time), target_time)
        if self.minutes(self.time) is target_time:
            print("here") # See if statement is triggered
            
class B(A):
    def __init__(self):
        super().__init__()

    def runner(self):
        while(True): # Keep updating self.time and calling the call method 
            time.sleep(.2)
            self.update_time()
            self.call()

B().runner()

Aucun commentaire:

Enregistrer un commentaire