Skip to content
Exercise · First program · lesson 7 of 7

Exercise: rectangle perimeter

Write a function perimeter(width, height) that returns the perimeter of a rectangle with sides width and height. The perimeter is the sum of the lengths of all four sides.

  • perimeter(3, 5) → 16
  • perimeter(1, 1) → 4
  • perimeter(0, 7) → 14

Constraints: the sides are integers from 0 to 1000, and either of them may be zero.

The "Run tests" button runs the checks from the test_rectangle.py tab—you can read them but not change them. Each check is a test_… function: it calls perimeter, and the line with assert demands that the answer equal the expected one. If it does not, the check shows got and what your function returned.

None in the stub means "nothing yet"—replace the whole return None line.

Do not add extra imports unless the task says so explicitly.

rectangle.py — python
python · 3 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way