Learning Python

Need help with this question? any tips would be greatly appreciated.

(Modify this code such that a_squared and b_squared are computed without calling square . )

from math import sqrt

def square(n):
return n * n

def hypotenuse(a, b):
a_squared = square(a)
b_squared = square(b)
return sqrt(a_squared + b_squared)

result = hypotenuse(3.0, 4.0)

print(result).

You have two variables that need to be squared. What operators and expressions can be used to square the value of a variable?

I am very new to this and being self taught, i am currently stuck on this question.

Your square function has this:

    return n * n

So outside the function, you could do this to square the value of a, instead of calling the function:

a_squared = a * a

thank you! I was reading way to much into it, thinking i needed the sqrt still… I appreciate the help

Though you don’t need to call square, you still need sqrt.

EDIT (October 30, 2021):

… unless you would like to change this …

    return sqrt(a_squared + b_squared)

… to this …

    return (a_squared + b_squared) ** 0.5
1 Like

I see what you are saying, thank you!

1 Like