0

Hey I was wondering why this first piece of code is different than the second, the first piece of code takes in True and outputs 1 while the second one correctly outputs "nope". To me both if statements should produce the same output.

1.

def distance_from_zero(Number):
    if type(Number) == int or float:
        return abs(Number)
    else:
        return "Nope"

2.

def distance_from_zero(Number):
    if type(Number) == int or type(Number) == float:
        return abs(Number)
    else:
        return "Nope"
Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
  • Note that the canonical way to do this in Python is `if isinstance(Number, (int, float))` – Daniel Roseman Oct 10 '15 at 14:28
  • The if statement is evaluated like so (For 1 and 2): 1 -> `if (type(number) == int) OR float`, or 2 -> `if (type(number) == int) OR (type(number) == float)` The first is checking for the truthfulness of `float`. – JosephGarrone Oct 10 '15 at 14:29

0 Answers0