-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrectangle.py
More file actions
48 lines (37 loc) · 1.31 KB
/
rectangle.py
File metadata and controls
48 lines (37 loc) · 1.31 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
def calculate_rectangle_properties(width, height):
"""
Calculate the area and perimeter of a rectangle.
Args:
width (float): Width of the rectangle
height (float): Height of the rectangle
Returns:
tuple: (area, perimeter)
"""
area = width * height
perimeter = 2 * (width + height)
return area, perimeter
def main():
try:
# Get input from user
print("Rectangle Calculator")
print("-" * 20)
width = float(input("Enter the width of the rectangle: "))
height = float(input("Enter the height of the rectangle: "))
# Validate input
if width <= 0 or height <= 0:
print("Error: Width and height must be positive numbers.")
return
# Calculate properties
area, perimeter = calculate_rectangle_properties(width, height)
# Print results
print(f"\nRectangle Properties:")
print(f"Width: {width}")
print(f"Height: {height}")
print(f"Area: {area}")
print(f"Perimeter: {perimeter}")
except ValueError:
print("Error: Please enter valid numbers for width and height.")
except KeyboardInterrupt:
print("\nProgram interrupted by user.")
if __name__ == "__main__":
main()