Code for solution with comments:
def hypotenuse(leg1,leg2): #our function has 2 arguments for first leg and second leg respectively
return (leg1**2+leg2**2)**(1/2) #according to Pythagorean theorem, the hypotenuse is equal to the square root of the sum of the squares of the legs
print("Test number 1, triangle with legs 3 and 4, hypotenuse is:",hypotenuse(3,4))#prints the call for triangle with legs 3 and for
print("Expected 5")#expected result
print("Test number 1, triangle with legs 12 and 5, hypotenuse is:",hypotenuse(12,5))#same for 12 and 5
print("Expected 13")
print("Test number 1, triangle with legs 20 and 21, hypotenuse is:",hypotenuse(20,21))#20 and 29
print("Expected 29")
Output of the code:
Test number 1, triangle with legs 3 and 4, hypotenuse is: 5.0
Expected 5
Test number 1, triangle with legs 12 and 5, hypotenuse is: 13.0
Expected 13
Test number 1, triangle with legs 20 and 21, hypotenuse is: 29.0
Expected 29
Comments
Leave a comment