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

Asked By: Mohammad Amir

||

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/

Answered By: Issa_newton1995
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.