/
28.geometry.py
60 lines (53 loc) · 1.97 KB
/
28.geometry.py
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
# This program allows the user to choose various
# geometry calculations from a menu. This program
# imports the circle and rectangle modules.
import circle
import rectangle
import math
# Constants for the menu choices
AREA_CIRCLE_CHOICE = 1
CIRCUMFERENCE_CHOICE = 2
AREA_RECTANGLE_CHOICE = 3
PERIMETER_RECTANGLE_CHOICE = 4
QUIT_CHOICE = 5
# The main function.
def main():
# The choice variable controls the loop
# and holds the user's menu choice.
choice = 0
while choice != QUIT_CHOICE:
# display the menu.
display_menu()
# Get the user's choice.
choice = int(input('Enter your choice: '))
# Perform the selected action.
if choice == AREA_CIRCLE_CHOICE:
radius = float(input("Enter the circle's radius: "))
print('The area is', circle.area(radius))
elif choice == CIRCUMFERENCE_CHOICE:
radius = float(input("Enter the circle's radius: "))
print('The circumference is', \
circle.circumference(radius))
elif choice == AREA_RECTANGLE_CHOICE:
width = float(input("Enter the rectangle's width: "))
length = float(input("Enter the rectangle's length: "))
print('The area is', rectangle.area(width, length))
elif choice == PERIMETER_RECTANGLE_CHOICE:
width = float(input("Enter the rectangle's width: "))
length = float(input("Enter the rectangle's length: "))
print('The perimeter is', \
rectangle.perimeter(width, length))
elif choice == QUIT_CHOICE:
print('Exiting the program...')
else:
print('Error: invalid selection.')
# The display_menu function displays a menu.
def display_menu():
print(' MENU')
print('1) Area of a circle')
print('2) Circumference of a circle')
print('3) Area of a rectangle')
print('4) Perimeter of a rectangle')
print('5) Quit')
# Call the main function.
main()