[Tutor] if statement problems(noob question)

Steven D'Aprano steve at pearwood.info
Mon Apr 8 14:18:06 CEST 2013


On 08/04/13 20:21, Max Smith wrote:
> Hi, everyone whom might read this, im Max and i am really new to coding,
> been going at it for about two weeks using codeacademy and the book "think
> python".
> when i decided to experiment a little with if statements i ran into the
> following problem:
>
> def plus(x,y):
>      if x and y == int or float:
>          print "The answer is: " + str(x+y)
>      else:
>          print "not a number"


Your problem here is that you've let Python's friendly syntax fool you into
thinking you can program in English. Python's good, but it's not that good!


In English, I might say to you:

"If x and y are ints, or floats, then do this ..."

which you've translated into Python as:

if x and y == int or float: ...


But that's not what it means to Python. What Python does is quite different.
Translating the above:

if x is a truthy value,
    and y equals the function "int",
    or the function "float" is a truthy value,
then ...


What do I mean by "is a truthy value"?

Python has a rule that *every* value, numbers, strings, lists, etc, everything,
can be considered "true-like" or "false-like". The rules are:

- values which are considered "empty" or "nothing" in some sense are treated
   as false-like:

   zero numbers: 0, 0.0
   empty string: ''
   empty containers (lists, dicts, tuples)
   None
   False

   are all "false-like" or "falsey" values.


- values which are considered non-empty, or "something", are treated as
   true-like:

   every number except zero, e.g. 0.0001, 42, -17.4
   every non-empty string, e.g. "hello world"
   every non-empty container, e.g. [1, 2, 3]
   True
   functions
   modules
   etc.

   are all "true-like" or "truthy" values.


So let's go back to your code:

if x and y == int or float: ...


1) First, Python checks whether x is a truthy value. In the examples you give,
it is, so the if-clause starts to look like this:

     if True and y == int or float: ...


2) Next, Python checks whether y equals the int function. In this case, it does
not, so the if-clause looks like this:

     if True and False or float: ...

and Python can simplify "True and False" to just False:

     if False or float: ...


3) Next, Python checks whether the float function is a truthy value. Since
functions are "something" rather than "nothing", it is considered truthy, and
the if-clause looks like this:

     if False or True: ...

which Python simplifies to:

     if True: ...


and so the if-clause *always* evaluates as True.



So what do you have to write instead?

Firstly, you can't check the type of a value with equals. You use the isinstance
function instead. To check if x is an int:

if isinstance(x, int): ...


To check if x is an int, or a float, make the second argument a tuple of types:


if isinstance(x, (int, float)): ...


To check is x *and* y are ints, or floats, you have to check each one separately:


number = (int, float)  # tuple of two types
if isinstance(x, number) and isinstance(y, number): ...




Try that, and see how your code works.





-- 
Steven


More information about the Tutor mailing list