SarcasticSully
SarcasticSully

Reputation: 462

Why is the expression false and true evaluating to true? (python 2.7)

I have two boolean variables in my program called after and late. Just to make sure each variable got the right value, I tested each one with the following command:

print(after)
print(late)

The program prints

false
true

as expected. However, when I run the following code:

if after and late:
  print('true and true')
elif after and (not late):
  print('true and false')
elif (not after) and late:
  print('false and true')
elif (not after) and (not late):
  print('false and false')

the program prints

'true and true'

which means that the expression after and late is evaluating to true. Why is this evaluating to true even though true and false should yield false?

Upvotes: 0

Views: 665

Answers (1)

wenzul
wenzul

Reputation: 4048

>>> print(True and False)
False

Boolean values have a capital letter in the beginning. I guess you use strings. You could check that with type(after).

You don't need to divide all the cases manually to "debug" your program. That's not the way Python was intended for... Just print the evaluated code print(after+' and '+late), use type like me above or use your interactive python console to play around.

Upvotes: 4

Related Questions