Return the type of a given value
Write a function using if
statements that takes a value, and returns the type of that particular value. Example:
which_type(2) == 'integer'
which_type("cookies, now!") == 'string'
which_type(False) == 'boolean'
which_type(42.0) == 'float'
Hint: Be mindful of control flow. Remember that booleans in python are tricky.
Test Cases
Solution 1
def which_type(val):
if isinstance(val, str):
return 'string'
elif isinstance(val, bool):
return 'boolean'
elif isinstance(val, int):
return 'integer'
elif isinstance(val, float):
return 'float'