Pythagorean theorem

Works out the hypotenuse of a right triangle from its two legs. Teaches the square root sqrt and the power **.

Open in Tokbit Level: Beginner. Free in the browser, nothing to install, no account needed.

Run the example in Tokbit and step through it: before each step say what will happen and check it in the Variables panel. Then change it your way.

Flowchart

Flowchart of the Pythagorean theorem example

Python program

The same program as Python code. In Tokbit you see it when you switch from Diagram to Python above the flowchart.

"""Works out the hypotenuse from the two legs of a right triangle. Teaches square roots and powers."""
from math import sqrt

a: float
b: float
# Asks again until the size is positive.
while True:
    a = float(input("Leg a:"))
    if a <= 0:
        print("The size must be greater than 0.")
    if not (a <= 0):
        break
while True:
    b = float(input("Leg b:"))
    if b <= 0:
        print("The size must be greater than 0.")
    if not (b <= 0):
        break
# Pythagorean theorem: c² = a² + b². hypot(a, b) gives the same.
c: float = sqrt(a ** 2 + b ** 2)
print("Hypotenuse c =", round(c, 2))

Follow-up tasks

Tasks in which you change the example (with a model solution):

All examples