how to create a function that has three parameters, but only use two of each of these parameters in python

Asked

Viewed 43 times

-2

I need to create a function that calculates the trigonometric ratios between the sides of a rectangular triangle. The function should receive three parameters: opposite cathode, adjacent cathode and hypotenuse. When performing the function, only two of the three parameters should receive values. The function will return the value for Sine, Cosine or Tangent according to the values received.

1 answer

0


You can do using one of the parameters being zero, and make a condition to return the result based on which parameter equals zero.

def calcularTriangulo(catetoOp,catetoAdj,hipotenusa):
if (catetoOp == 0):
    # retorna Cosseno
    print(catetoAdj/hipotenusa)
elif (catetoAdj == 0):
    # retorna Seno
    print(catetoOp/hipotenusa)
elif (hipotenusa == 0):
    # retorna Tangente
    print(catetoOp/catetoAdj)

So for example, to calculate the tangent would be:

calcularTriangulo(3,4,0) -- Tangent

Same thing for Seno and Cosseno:

calcularTriangulo(0,4,5) -- Cosine

calcularTriangulo(3,0,5) -- Sine

  • 1

    Thank you! Right at the beginning the text was almost a paradox, but then I understood how it worked, I found the answer. but thanks for the good itenção

Browser other questions tagged

You are not signed in. Login or sign up in order to post.