IniziaInizia gratis

Create a tool for math calculations

It's time to build your tool. Let's imagine you run a small construction company and need to calculate the length of one side of a roof. If you know the lengths of two beams that support the roof at a right angle, you can use their lengths to calculate the length of the roof using the hypotenuse formula below. $$c = \sqrt{a^2 + b^2}$$

Roof beam calculation diagram

Questo esercizio fa parte del corso

Designing Agentic Systems with LangChain

Visualizza il corso

Istruzioni dell'esercizio

  • Use the necessary decorator to define the function as a tool.
  • To find the hypotenuse, use the .split() method on the input string to extract the two other lengths of a right-sided triangle.
  • Convert each triangle side, a and b, to floats and use .strip() to remove any extra spaces from the values.
  • Use Python's math module to square the lengths a and b, sum their values, and find their square root to reveal the length of the roof.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# Define this math function as a tool
____
def hypotenuse_length(input: str) -> float:
    """Calculates the length of the hypotenuse of a right-angled triangle given the lengths of the other two sides."""
    
    # Split the input string to get the lengths of the triangle
    sides = ____.____(',')
    
    # Convert the input values to floats, removing extra spaces
    a = ____(____[0].____())
    b = ____(____[1].____())
    
    # Square each of the values, add them together, and find the square root 
    return ____.sqrt(a____2 + b____2)
Modifica ed esegui il codice