Rectangle-Or-Square Constructor With Smart Defaults

Medium ⏱ 12 min 49% acceptance ★★★★★ 4.6
Write a function make_rectangle(width, height=None) that returns a tuple (width, height). If height is not supplied (left as None), the function should treat the shape as a square and use width for both dimensions. Note that you cannot reference width directly as a default expression (height=width is illegal at definition time), so use None as a sentinel and resolve it inside the body.

Examples

Example 1
Input
make_rectangle(5)
Output
(5, 5)
Explanation

No height given, so it becomes a square with both sides 5.

Example 2
Input
make_rectangle(5, 3)
Output
(5, 3)
Explanation

Both dimensions explicitly supplied.

Constraints

  • height defaults to None, never directly to another parameter name.

Topics

Functionsdefault arguments

Companies

SwiggyOla

Hints

Hint 1

A default value can only be a constant/expression fixed at definition time — it cannot reference a sibling parameter, so None is the standard sentinel.

Hint 2

Inside the function, do `if height is None: height = width`.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.