From e3302bd257f746e4589853461f2775bc43383713 Mon Sep 17 00:00:00 2001 From: minhngoc15998 Date: Wed, 18 Mar 2026 22:26:31 -0700 Subject: [PATCH] Update area_of_rectangle.py --- area_of_rectangle.py | 59 ++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 35 deletions(-) diff --git a/area_of_rectangle.py b/area_of_rectangle.py index 52b5b71..f2ea978 100755 --- a/area_of_rectangle.py +++ b/area_of_rectangle.py @@ -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 @@ -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).")