Chapter 1: The Basics / Lesson 10

Hypotenuse Calculator

10 min · The Basics

🎯 Project: Hypotenuse Calculator

Let's apply what we've learned! We'll build a program that calculates the hypotenuse of a right triangle using the Pythagorean theorem. This project combines variables, type conversion, the math module, and string formatting.

The Pythagorean theorem states: c² = a² + b², which means: c = √(a² + b²)

Where:

  • a and b are the two shorter sides (legs) of the right triangle
  • c is the hypotenuse (the longest side, opposite the right angle)

Project Requirements

Your program should:

  • Get two sides of a right triangle (a and b) - for this exercise, use provided values
  • Calculate the hypotenuse using the Pythagorean theorem
  • Round the result to 2 decimal places for clean output
  • Display the result in a formatted, user-friendly message

Step-by-Step Guide

Here's how to approach this problem:

steps.py
# Step 1: Import the math module import math # Step 2: Define the two sides a = 3 b = 4 # Step 3: Calculate a² and b² a_squared = a ** 2 # 9 b_squared = b ** 2 # 16 # Step 4: Add them together sum_of_squares = a_squared + b_squared # 25 # Step 5: Take the square root hypotenuse = math.sqrt(sum_of_squares) # 5.0 # Step 6: Round to 2 decimal places hypotenuse_rounded = round(hypotenuse, 2) # Step 7: Display the result print(f"For a right triangle with sides {a} and {b},") print(f"the hypotenuse is: {hypotenuse_rounded}")

Optimized Solution

You can combine steps to make the code more concise:

optimized.py
import math # Define the sides a = 3 b = 4 # Calculate hypotenuse in one line hypotenuse = math.sqrt(a ** 2 + b ** 2) # Round and display hypotenuse_rounded = round(hypotenuse, 2) print(f"The hypotenuse is: {hypotenuse_rounded}")

Testing Your Solution

Test your program with known values to verify it works correctly:

test_cases.py
import math # Test case 1: Famous 3-4-5 triangle a, b = 3, 4 c = round(math.sqrt(a**2 + b**2), 2) print(f"a={a}, b={b}, c={c}") # Should be 5.0 # Test case 2: 5-12-13 triangle a, b = 5, 12 c = round(math.sqrt(a**2 + b**2), 2) print(f"a={a}, b={b}, c={c}") # Should be 13.0 # Test case 3: Non-integer result a, b = 1, 1 c = round(math.sqrt(a**2 + b**2), 2) print(f"a={a}, b={b}, c={c}") # Should be 1.41 (√2)

Hints and Tips

💡 Key Functions to Use

import math - Import the math module

math.sqrt(x) - Calculate square root

x ** 2 - Square a number (x²)

round(value, 2) - Round to 2 decimal places

f"text {variable}" - Format strings for output

💡 Common Mistakes to Avoid

• Forgetting to import math

• Not squaring the sides before adding

• Taking square root before adding a² and b²

• Forgetting to round the result

Challenge: Extended Version

Once you complete the basic version, try enhancing it:

  • Calculate and display the area of the triangle (area = 0.5 × a × b)
  • Calculate and display the perimeter (perimeter = a + b + c)
  • Test with different values and verify results
  • Format all numbers to 2 decimal places for consistency

💡 Famous Example

For a triangle with sides a=3 and b=4, the hypotenuse should be 5.0. This is the famous 3-4-5 right triangle - one of the most well-known Pythagorean triples! Try it: 3² + 4² = 9 + 16 = 25, and √25 = 5.

🎉

Lesson Complete!

Great work! Continue to the next lesson.

main.py
📤 Output
Click "Run" to execute...