Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 24 additions & 35 deletions area_of_rectangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,13 @@

import sys


def area_of_rectangle(height, width = None):
def area_of_rectangle(height, width=None):
"""
Returns the area of a rectangle.

Parameters
----------
height : int or float
The height of the rectangle.
width : int or float
The width of the rectangle. If `None` width is assumed to be equal to
the height.

Returns
-------
int or float
The area of the rectangle

Examples
--------
>>> area_of_rectangle(7)
49
>>> area_of_rectangle (7, 2)
14
"""
if width:
if width is None:
width = height

area = height * width
return area

Expand All @@ -41,15 +21,24 @@ def area_of_rectangle(height, width = None):
"\tthe height of a square or the height and width of a "
"rectangle".format(script_name = sys.argv[0]))
sys.exit(message)
height = sys.argv[1]
width = height
if len(sys.argv) > 3:
width = sys.argv[1]

area = area_of_rectangle(height, width)

message = "The area of a {h} X {w} rectangle is {a}".format(
h = height,
w = width,
a = area)
print(message)

try:
height = float(sys.argv[1])

if len(sys.argv) == 3:
width = float(sys.argv[2])
else:
width = None

area = area_of_rectangle(height, width)

actual_width = width if width is not None else height

message = "The area of a {h} X {w} rectangle is {a}".format(
h = height,
w = actual_width,
a = area)
print(message)

except ValueError:
sys.exit("Error: Please provide numeric values (e.g., 7 or 7.5).")