How can I check if the type is int or float?
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() accepts one argument, as we know, and returns its type as either
str, among others. It does not return it as a string like
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(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
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.