Functions 19/19 - Question


#1

Hello,

Question regarding the formatting of Functions 19/19.

My first crack at this problem resulted in the following text:

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

I see now that the correct code for line 2 should be:

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

However, I would really appreciate if someone could explain why the first example doesnt work :slight_smile:
Asking if type(n) is equal to either an integer or a float, seams to logically make sense.


#2

An object is truthy. and since a class is an object,

>>> print (float)
<class 'float'>
>>> print (int)
<class 'int'>
>>> print (True if float else False)
True
>>> print (True if int else False)
True
>>> 

In this code,

because float is always True, the expression will never yield False.

Your second example has two distinct comparison expressions as operands so can yield False.


#4

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