← All coding practice

Coding Practice · Beginner · October 11, 2026 · 5 min read

Python Coding Exercise #27: Hypotenuse of a Right-Angled Triangle

Part of the series Python Practice Exercise — part 27

Before you start: Know input() and float() · Know the ** operator and f-strings

The question

Write a program that enters the base and height of a right-angled triangle and finds its hypotenuse according to Pythagoras’ theorem: Hypotenuse² = Base² + Height². Use the sqrt function from the math module.

For example, for a triangle with a base of 3 and a height of 4:

Enter the base: 3
Enter the height: 4
The hypotenuse is 5.00 cm

The sides can have decimals too:

Enter the base: 1.5
Enter the height: 2
The hypotenuse is 2.50 cm

My Solution

from math import sqrt

base = float(input("Enter the base: "))
height = float(input("Enter the height: "))
hypotenuse = sqrt((base ** 2) + (height**2))
print(f"The hypotenuse is {hypotenuse:.2f} cm")

How My Solution Works

1. Pythagoras’ theorem

In a right-angled triangle, the hypotenuse is the longest side, the one opposite the right angle. Pythagoras’ theorem says that if you square the two shorter sides and add them, you get the square of the hypotenuse:

hypotenuse² = base² + height²

To find the hypotenuse itself, take the square root of both sides:

hypotenuse = √(base² + height²)

For a base of 3 and a height of 4, that’s 3² + 4² = 9 + 16 = 25, and √25 = 5.

2. Import sqrt

from math import sqrt

sqrt() is a function from the math module that returns the square root of a number. This line brings in just that function, so you can call it by its plain name. (The same style was used for pi in Exercise #22 and for gcd in Exercise #25.) Note that sqrt() always returns a float, even for a perfect square: sqrt(25) is 5.0, not 5.

3. User input

base = float(input("Enter the base: "))
height = float(input("Enter the height: "))

Each side is read with float(input(...)), which converts the typed text to a number that can have a decimal part. The sides of a triangle don’t have to be whole numbers, so float() is the better choice here than int(). It still accepts whole numbers: typing 3 gives 3.0. (If the user types something that isn’t a number, such as abc, the program stops with a ValueError.)

4. Calculate the hypotenuse

hypotenuse = sqrt((base ** 2) + (height**2))

The ** operator means “to the power of”, so base ** 2 is the base squared. (Both ** and the order of operations were covered in Exercise #22.) The brackets around the two squares aren’t needed, because ** is calculated before + anyway. They do no harm, and I added them to make the formula easier to read.

5. Print the result

print(f"The hypotenuse is {hypotenuse:.2f} cm")

The :.2f after the variable name is a format. It means “show this float with exactly two digits after the decimal point”. That’s why the output reads 5.00 cm rather than 5.0 cm, and why a 1 and 1 triangle prints 1.41 cm rather than the long number 1.4142135623730951.

Here are some triangles whose hypotenuse comes out as a whole number:

BaseHeightOutput
34The hypotenuse is 5.00 cm
512The hypotenuse is 13.00 cm
68The hypotenuse is 10.00 cm
815The hypotenuse is 17.00 cm

Sets of whole numbers like these, where all three sides are whole, are called Pythagorean triples.

A catch

You might be tempted to use int() to make the output look clean, for example int(hypotenuse). But you need to keep in mind that the hypotenuse can also be a decimal number. Most right-angled triangles do not have a whole-number hypotenuse, and int() doesn’t round. It simply cuts off everything after the decimal point. Here is what the program would print if int() were used, next to the true value:

BaseHeightProgram printsTrue hypotenuse
111 cm1.4142135623730951
233 cm3.605551275463989
557 cm7.0710678118654755
101014 cm14.142135623730951

These answers are all slightly wrong, and there’s no error message to warn you. The program only gives the correct result when the hypotenuse happens to be a whole number. This is the kind of bug that’s easy to miss, because the first examples you try (3 and 4, or 5 and 12) work perfectly.

The fix: keep the decimals

That is why My Solution uses :.2f instead of int(). It keeps the decimals, rounded to two places, so the output is correct for every triangle:

BaseHeightOutput
34The hypotenuse is 5.00 cm
11The hypotenuse is 1.41 cm
23The hypotenuse is 3.61 cm
55The hypotenuse is 7.07 cm
1.52The hypotenuse is 2.50 cm

You could also use round(hypotenuse, 2). It gives 1.41 for the 1 and 1 triangle, but it prints 5.0 instead of 5.00 for the 3 and 4 triangle, because round() drops trailing zeros. (This difference was explained in Exercise #22.)

Other ways to calculate it

Using hypot(). The math module has a function that calculates the hypotenuse directly, without squaring and adding yourself:

from math import hypot

print(hypot(3, 4))   # 5.0

It gives the same answer as sqrt(3 ** 2 + 4 ** 2). The exercise asks for sqrt, but hypot() is worth knowing.

Using a power of 0.5. A square root is the same as raising to the power of one half, so this also works without any import:

print((3 ** 2 + 4 ** 2) ** 0.5)   # 5.0

Key takeaways

  • In a right-angled triangle, hypotenuse² = base² + height², so hypotenuse = sqrt(base ** 2 + height ** 2).
  • sqrt() is in the math module, and it always returns a float.
  • Use float(input(...)) when the values can have decimals, such as the sides of a triangle.
  • int() cuts off the decimal part. It doesn’t round. For most triangles, the hypotenuse isn’t a whole number, so int() gives a slightly wrong answer without any warning.
  • Show decimals with :.2f (always two decimal places) or round(value, 2).
  • Test your program with more than the easy examples. A result that works for 3 and 4 may fail for 1 and 1.
  • math.hypot(a, b) calculates the hypotenuse directly.

✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦


Practice Source: This exercise is taken from Ultimate Python Programming: Learn Python with 650+ Programs, 900+ Practice Questions, and 5 Projects by Deepali Srivastava, published by BPB Publications. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.

Tagged: python · math-module · input · float · arithmetic