input()
in Python 3 returns a string. No data conversions are performed. So the inputs you get are, literally, the strings "True"
and/or "False"
. Then bool()
applied to a non-empty string returns True
.
>>> bool("True")
True
>>> bool("False")
True
>>> bool("just about anything")
True
>>> bool("") # except an empty string
False
That's why you always get 2 no matter what you enter. Both inputs become True
, which equals 1 in a numeric context.
>>> True == 1
True
While people will yell at you if you do this, eval()
is the most convenient (although dangerous!) way to evaluate a string as if it were Python code:
>>> eval("True")
True
>>> eval("False")
False
It's dangerous because eval()
will evaluate any Python expression, so if you don't trust your input it can trick your program into doing just about anything.