Reputation: 131
Hi I'm an intermediate python user and I came across this strange bug. I tested it on both a Mac and Windows 10 computer and got the same results.
Here is my code:
def t1(time):
return '21:00:00' < time
def t2(time):
return '05:00:00' > time
def runScript(time):
return '21:00:00' < time or '5:00:00' > time
def runScript2(time):
return t1(time) or t2(time)
t = '15:00:00'
print(t1(t))
print(t2(t))
print(runScript(t))
print(runScript2(t))
Here are my results:
False
False
True
False
This is what I get running it straight from the interpreter
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def t1(time):
... return '21:00:00' < time
...
>>> def t2(time):
... return '05:00:00' > time
...
>>> def runScript(time):
... return '21:00:00' < time or '5:00:00' > time
...
>>> def runScript2(time):
... return t1(time) or t2(time)
...
>>> t = '15:00:00'
>>>
>>> print(t1(t))
False
>>> print(t2(t))
False
>>> print(runScript(t))
True
>>> print(runScript2(t))
False
>>> exit()
Thank you!
Upvotes: 1
Views: 458
Reputation: 581
I guess you misinterpreted how string comparisons work in python and this clouded the actual solution for your problem.
You're trying to use date and time logic with strings, which is not recommendable. When using strings and operators you're doing a Lexicographical comparison:
Lexicographical ordering for strings uses the Unicode code point number to order individual characters.
So answering your first question, is not the boolean comparison in python that is wrong but how you wrote it:
def t2(time):
# Here you wrote 05:00:00 as your string
return '05:00:00' > time
def runScript(time):
# Here you wrote 5:00:00
return '21:00:00' < time or '5:00:00' > time
In this case, '5:00:00' is different and greater them '05:00:00' as your interpreter is comparing these two strings not as a time data type but as a generic string.
# Using python 3.8
'05:00:00' > '5:00:00'
False
'5:00:00' > '05:00:00'
True
Python has a great datetime lib and you can find it's reference here: https://docs.python.org/3/library/datetime.html
from datetime import datetime
# datetime.strptime(date_string, format) convert's a string into a datetime object
def t1(time):
time_string_t1 = '21:00:00'
datetime_object_t1 = datetime.strptime(time_string_t1, '%H:%M:%S')
return datetime_object_t1 < time
def t2(time):
time_string_t2 = '05:00:00'
datetime_object_t2 = datetime.strptime(time_string_t2, '%H:%M:%S')
return datetime_object_t2 > time
def runScript(time):
return datetime.strptime('21:00:00', '%H:%M:%S') < time or datetime.strptime('05:00:00', '%H:%M:%S')> time
def runScript2(time):
return t1(time) or t2(time)
t = datetime.strptime('15:00:00', '%H:%M:%S')
print(t1(t))
print(t2(t))
print(runScript(t))
print(runScript2(t))
Here I'm comparing two datetime objects, and the result was:
python test.py
False
False
False
False
Please take a look in this solution the biggest problem in your code is that you're using the wrong data type for the problem you're trying to solve not the boolean logic.
Other useful references:
Upvotes: 2
Reputation: 486
change this line from
return '21:00:00' < time or '5:00:00' > time
to
return ('21:00:00' < time) or ('05:00:00' > time)
here the problem is you are not actually comparing a DateTime object so you can't expect 05:00:00 and 5:00:00 be same
Upvotes: 0
Reputation: 77912
You are comparing strings. Strings compare with respect to lexical ordering, not to numerical ordering, and '5:00:00'
(in runScript()
is NOT the same as '05:00:00'
(in t2()
)
Upvotes: 0
Reputation: 795
In your runScript
the second condition you are checking with '5:00:00' > time
which supposed to be '05:00:00' > time
because
('5:00:00' > '15:00:00') == True
Change your function to which is as function t2
>>> def runScript(time):
... return '21:00:00' < time or '05:00:00' > time # '5:00:00'
Then it would be,
(False or False) == False
Upvotes: 1