How can I check if the type is int or float?

Question

How can I check if the type is int or float?

Answer

To determine whether or not the value given to the function is an int or a float, we need to understand what is being returned to us by the type() function.
type() accepts one argument, as we know, and returns its type as either int, float, or str, among others. It does not return it as a string like ”int”.
If your code looked like this:

def my_function(value):
  if type(value) == “str”:
    return “It’s a string!”

That would be invalid! type() will never return ”str”, so it cannot possibly enter that conditional statement.
Another common error is misusing type()’s argument. For example, if you checked for type(int) or type(float), that’s not quite what we’re trying to do in this exercise. Be sure to give the argument given to your function as the argument for type().
Lastly, keep in mind that == is used for comparison, while = is used to assign values. If you use = in an expression being checked, you will get an error.

4 Likes

I don’t get it. Why does

print type(5)

result in <type ‘int’> and not just int ?

Thanks in advance

2 Likes

2 posts were split to a new topic: Why Do We Check Datatypes Without Quotes?

I’d suggest to check the type of ‘n’ in if statements using this :

if type(n) is int:
if type(n) is float:
if type(n) is str:
1 Like

Can’t you use the .isdigit keyword that codeacademy previously introduced us to?

that is just the format

1 Like

7 posts were split to a new topic: How can I set the value to test the function

Why wouldn’t I be able to type it like this:

if type(n) == int or float:

Why is the formatting necessary such that I have to type it

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

Thank you advance.

from a human point of view, this makes a lot of sense. Not for the computer.

on the other hand, it allows us to do:

if x == 5 or y:

so either x has to be 5, or y has to be a truthy value.

of course, you could overcome your problem by doing:

if type(n) in (int, float):
4 Likes

Alright, thank you kindly for replying.

Hello, I don’t understand why my first line isn’t valid
here is my code

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

you forgot the colon (:) at the end of your function “deceleration”.

Trying to define a function with the incorrect syntax will lead to a syntax error

1 Like

please i dont understand whats going on. somebody help me please.def distance_from_zero(distance):
if type(distance) == int or type(distance) == float:
print abs(distance)
return abs(distance)
else:
print “nope”
return “nope”
distance_from_zero(10)

its printing many numbers and nope at the end

What is it you plan to achieve?

1 Like

Hi,

Can one of you folks explain to me why num = input() wouldn’t work in this?

def distance_from_zero(num):
num = input()
if type(num) == int or type(num) == float:
return abs(num)
else:
return “No”

Seems a waste to have a parameter only to wipe it off the map. The user input should be outside of the function.

num = input()
print (distance_from_zero(num))
1 Like

Makes sense, Thanks!

1 Like

i want to return “nope”

i try to write some code:

num = 11
print (distance_from_zero())

but start to running.it be the error :

Output:
Traceback (most recent call last):
File “python”, line 8, in
TypeError: distance_from_zero() takes exactly 1 argument (0 given)

how should i fix?

The error message says it all. You are trying to call a function without supplying it with the correct number of arguments. Passing functions with arguments to another function in Python? - Stack Overflow.

However, there is an exception case:

def distance_from_zero(initial = 0.0):
  pass

print(distance_from_zero())

What do you think the output of code above would look like? Or will there be an output at all ? Do test it out and everything will become much clearer.