-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRectangleArea.py
More file actions
90 lines (70 loc) · 2.87 KB
/
RectangleArea.py
File metadata and controls
90 lines (70 loc) · 2.87 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import time
def draw_rectangle(length, width):
"""Draws a rectangle with the given length and width using '=' characters."""
for i in range(length):
for j in range(width):
time.sleep(.3)
print("=", end="")
print()
def main():
"""Main entry point for the program."""
# Get user input for length and width
length_input = int(input("Enter the length of the rectangle 1-10: "))
width_input = int(input("Enter the width of the rectangle 1-10: "))
divider = "====================="
#calculate the area
area = length_input * width_input
perim = 2 * (length_input + width_input)
unit_choice = input("Press [1] for centimeters , [2] for inches , [3] for feet")
correct_user_choice = False
while not correct_user_choice:
if unit_choice == '1':
correct_user_choice = True
unit_choice = "centimeters"
elif unit_choice == '2':
correct_user_choice = True
unit_choice = "inches"
elif unit_choice == '3':
correct_user_choice = True
unit_choice = "feet"
else:
print(divider)
print("Invalid response, please input [1], [2], or [3]")
print(divider)
unit_choice = input("Press [1] for centimeters , [2] for inches , [3] for feet")
correct_user_choice = False
#to check initial input as valid
valid_length = 1 <= length_input <= 10
valid_width = 1 <= width_input <= 10
#logic
answered_correctly = valid_length and valid_width
# Display length and width
print(divider)
print("Length: ", length_input , unit_choice)
print("Width: ", width_input , unit_choice)
print(divider)
while not answered_correctly:
length_input = int(input("Please enter a valid length of the rectangle 1-10: "))
width_input = int(input("Please enter the width of the rectangle 1-10: "))
#reassign value
area = length_input * width_input
perim = 2 * (length_input + width_input)
# Recalculate the conditions for validity
valid_length = 1 <= length_input <= 10
valid_width = 1 <= width_input <= 10
# Update answered_correctly based on new input
answered_correctly = valid_length and valid_width
if answered_correctly:
break
else:
print("Please input a length value between 1 and 10.")
#Display area
print("Area: ", area , unit_choice, "^2" )
print("Perimeter: " , perim, unit_choice)
print("Unit: " , unit_choice)
print(divider)
# # Draw the rectangle
draw_rectangle(length_input, width_input)
if __name__ == "__main__":
# Call the main function if the script is executed directly
main()