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:
aandbare the two shorter sides (legs) of the right trianglecis 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:
Optimized Solution
You can combine steps to make the code more concise:
Testing Your Solution
Test your program with known values to verify it works correctly:
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.