loop
Creating multi-parameter functions

Module 4 · Creating multi-parameter functions · Lesson 2 of 5

Triangles

Here's a function with three parameters — three side lengths — and a real question to answer: do these sides actually form a triangle? The rule is the triangle inequality: each side must be shorter than the sum of the other two. All three checks have to hold.

is_triangle(3, 4, 5)
3 + 4 > 53 + 5 > 44 + 5 > 3
435

That “all three must hold” is exactly what and is for. The function combines the three comparisons into one Boolean and returns it — a clean True or False.

Run it yourselfruns in your browser · Python
main.py

Once you know the sides do make a triangle, a second three-parameter function can find its area with Heron's formula. Notice the pattern again: check first, then compute — two small functions, each with a single clear job.

Run it yourselfruns in your browser · Python
main.py

From here the problems get more mathematical — and introduce a pattern of their own. First up: factorials.