19 - Review built-in Functions


#1

I am not how my code is incorrect (please note I did import the math module just for good measure).

LESSON LINK
https://www.codecademy.com/courses/python-beginner-c7VZg/4/3?curriculum_id=4f89dab3d788890003000096#

CODE
import math
def distance_from_zero(num):
if num == int or num == float:
return abs(num)
else:
return "Nope"

ERROR MESSAGE
Oops, try again. Your function seems to fail on input -10 when it returned 'Nope' instead of '10'

OUTPUT
Honestly I'm not sure since the code did not require the function to be called?

For good measure I called the function and entered an argument. I still received the same error message.

import math
def distance_from_zero(num):
if num == int or num == float:
return abs(num)
else:
return "Nope"
distance_from_zero(5)

Any assistance would be appreciated.

Regards,

Melissa


#2

Hi Melissa!

So far, it looks like you're on the right track. There's a small problem with your conditional statements, however.

We're trying to compare the type of the number that is passed to the distance_from_zero() function, so let's look at how to do that.

In your conditional statement, you have:

if num == int or num == float:

The logic is great, but in this case, you're comparing num to type int and float. Instead, you should be comparing the type of num to the above types. Lucky for us, python has a built-in function called type() and it returns the type of a variable.

So, to correct your conditional statement, all you have to do is throw that function in there a couple times on num and it should be good to go:

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

Hope this helps your understanding.


#3

Thank you for your assistance. That was very helpful.

Regards,

Melissa


#4

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