Create a function called square that takes in a number and returns the square of that number
Question:
Question:- Create a function called square that takes in a number and returns the square of that number. If what’s passed in is not a float or an int, return "None"
Code:-
def square(x):
if x % 2 == 0:
return x**x
else:
return None
print(square(5))
Error:-
None !- 25 : square should return 25
Your Output
None
Answers:
why are you doing modulus 2 here?
def square(x):
if isinstance(x,int) or isinstance(x,float):
return x**2
else:
return none
print(square(5))
there are also other ways of checking of a number is a float/int or not
check this link for more
https://learnshareit.com/how-to-check-if-a-number-is-an-int-or-float-in-python/
Question:- Create a function called square that takes in a number and returns the square of that number. If what’s passed in is not a float or an int, return "None"
Code:-
def square(x):
if x % 2 == 0:
return x**x
else:
return None
print(square(5))
Error:-
None !- 25 : square should return 25
Your Output
None
why are you doing modulus 2 here?
def square(x):
if isinstance(x,int) or isinstance(x,float):
return x**2
else:
return none
print(square(5))
there are also other ways of checking of a number is a float/int or not
check this link for more
https://learnshareit.com/how-to-check-if-a-number-is-an-int-or-float-in-python/