-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy patharea_of_rectangle.py
More file actions
executable file
·70 lines (55 loc) · 1.55 KB
/
area_of_rectangle.py
File metadata and controls
executable file
·70 lines (55 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#! /usr/bin/env python3
"A script for calculating the area of a rectangle."
import sys
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
"""
height = int(height)
# print("height is:", height)
width = int(width)
# print("width is:",width)
if width == None:
width = height
area = height * width
return area
if __name__ == '__main__':
# print("len is ",len(sys.argv))
if (len(sys.argv) < 2) or (len(sys.argv) > 3):
message = (
"{script_name}: Expecting one or two command-line arguments:\n"
"\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]
# print("h is ",height)
if len(sys.argv) == 2:
width = height
else:
width = sys.argv[2]
#if len(sys.argv) > 3:
# width = sys.argv[1]
area = area_of_rectangle(height, width)
# print(area)
message = "The area of a {h} X {w} rectangle is {a}".format(
h = height,
w = width,
a = area)
print(message)