Rodrigo Ávila
Rodrigo Ávila

Reputation: 1

Python doesn't detect that two values are equal and returns false all the time

Sorry, I don't really know how to explain this. I am trying to create a little program that detects if the employees have entered to work at good time. First of all, with this I converted a float (that represents an hour) to a datetime value:

 estabHourF=(float(estabHour)+0.18)
 minutes = estabHourF*60
 hours, minutes = divmod(minutes, 60)
 print("%02d:%02d"%(hours,minutes))
 todaysYear = datetime.date.today().year
 todaysMonth = datetime.date.today().month
 todaysDay =datetime.date.today().day
 todaysSeconds = datetime.datetime.now().second

Then, I inserted all that into a list:

nowH = datetime.datetime(todaysYear, todaysMonth, todaysDay, int("%02d"%(hours)), int("%02d"%(minutes)), todaysSeconds)

Then, I created another list that creates like periods of minutes so I can compare now to the established hour to enter later on:

numMinutes = 15
date_list = [nowH - datetime.timedelta(minutes=x) for x in range(numMinutes)]

If I print this, it is like this:

datetime.datetime(2021, 7, 27, 9, 14, 33)

This is how it looks on the console.

Finally, I try to compare the list of time aproved to now by trying to imitate how it looks like:

for x in range(len(date_list)):
                if (date_list[checkList])=="datetime.datetime({0}, {1}, {2}, {3}, {4}, {5})".format(todaysYear, todaysMonth, todaysDay, int(datetime.datetime.now().strftime("%I")), datetime.datetime.now().minute, todaysSeconds):
                    punctual = True
                    print("Puntual: ", punctual)
                    print(datetime.datetime.now())
                else:
                    print("datetime.datetime({0}, {1}, {2}, {3}, {4}, {5})".format(todaysYear, todaysMonth, todaysDay, int(datetime.datetime.now().strftime("%I")), datetime.datetime.now().minute, todaysSeconds))
                checkList+=1

Yeah, if wrong, I wanted it to show me the same value that I am comparing, and, don't tell me this two aren't the same.

from my list: datetime.datetime(2021, 7, 27, 9, 25, 49)
from the print inside "else": datetime.datetime(2021, 7, 27, 9, 25, 49)

They are exactly the same, but it seems like python doesn't recognize it due to some of my mistakes :')

If you can help me, I would be really really thankful :'3

Upvotes: 0

Views: 672

Answers (2)

Uretki
Uretki

Reputation: 297

The "datetime.datetime(2021, 7, 27, 9, 14, 33)" you get when printing it is only a string representation of your datetime object. The datetime object itself is never equal to that string. You can directly compare two datetimes using comparison operators.

I am also not 100% sure of the purpose of your checklist variable but it seems to me that you are using it to iterate over date_list (I don't see it initialized in your snippet though). If this is the case, why not just use x or even better, you can directly do this:

for date in date_list:
    if date == nowH:
        #[...]

The date variable will in turn take all values from your list directly.

You can define your "latest acceptable datetime"

lateAfter = nowH + datetime.timedelta(minutes=numMinutes)

and then simply use comparison operators for dates by replacing your for with:

if datetime.datetime.now() <= lateAfter:
    punctual = True
    print("Puntual: ", punctual)
    print(datetime.datetime.now())
else: # Not punctual
    print(datetime.datetime.now())

Upvotes: 1

tehanton
tehanton

Reputation: 1

Do I understand correctly that the comparison that does not give you True is this?

if (date_list[checkList])=="datetime.datetime({0}, {1}, {2}, {3}, {4}, {5})".format(todaysYear, todaysMonth, todaysDay, int(datetime.datetime.now().strftime("%I")), datetime.datetime.now().minute, todaysSeconds):

Sorry if I'm making no sense (newbie) but aren't you comparing str with a datetime?

Upvotes: 0

Related Questions