Lesson 4 Exercise 19


#1

Lesson 4 Exercise 19: Review: Built-in functions
https://www.codecademy.com/courses/python-beginner-c7VZg/4/3?curriculum_id=4f89dab3d788890003000096


Error message: Oops, try again. Your function seems to fail on input True when it returned '1' instead of 'Nope'


For the bolded part of the code:

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

why can't I use if type(x) == int or float: instead? Why must I retype type(x) to not show the above error?


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


Review: Built-In Functions. type(n) == int or type(n) == float:
#2

Lets try to understand it..

type(x) == int or float ------------------------------------(1)

suppose,

First part if x is of int type..

x = 4

then type(x) == int will return True...
now we have True or float that'll return True
why ?
lets understand why ?

True or anything = True
If or operator sees that its first part is True,It does not care about second one and returns True.

even you do this...

True or djfjkfjkfkjjkf # It will return True

so your program works if it has i/p x = int type.

Second part when x is a float type..

x = 4.5

as in expression (1)

type(x) == int or float ------------------------------------(1)

type(x) == int # will return False as x is a float type.
One more step..

Now you have something like this...

False or float

Lets observe result from IDLE...

False or float returns <type 'float'>
Thus you can see your result is not a boolean value but <type 'float'>.
Your if statement was expecting a boolean value or a value that can return Truthy/Falsy ...so it fails at this point.
so your overall program does not work.

My explanation is based on observations as Its not a 'full logical explanation'.
Still, I hope it helps!


#3

Hi @toddthetyper99,

All programming languages that evaluate expressions involving various mathematical and other operations need to perform the operations in a defined order. This is necessary so that the values of expressions are not ambiguous.

Operator precedence charts specify the order in which operations are performed. See 5.15. Operator precedence. In that chart the operators are listed from lowest to highest precedence. Accordingly, the operations at the bottom are performed first. Note that the == operation is performed before the or operation. Therefore, this ...

    if type(x) == int or float:

... is equivalent to this ...

    if (type(x) == int) or float:

Therefore, if x is not an int, the portion of the expression to the right of the or is used, by the interpreter, as the value of the entire expression. The resulting value, float, when considered as a condition, corresponds to a True result, so the expression is, in effect, True. That is not acceptable, because it causes the if block to execute regardless of the value of x.

This works correctly, because it compares type(x) to int and then, if necessary, compares it to float ...

    if type(x) == int or type(x) == float:

Note, from the line at the bottom of the chart, that parentheses can be used to exert some control on the order of operations. However, in the current case, merely inserting parentheses will not correct the problem.

In some operator precedence charts, the order is listed with highest precedence at the top, so always read explanatory information that is provided with the chart.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.