# AreaCalculator_4_1171.py - version 4 # # This program allows us to compute the area of # a triangle, a circle, a rectangle, a square and an ellipse. # # Anne Lavergne: # Created June 2015 - Modified March 2017 # # Liaqat Ali: # Modified July 2018 # # Demonstrating "Incremental Development" # -> Refactoring repeated code from the functions square( ) and rectangle( ) # and encapsulating it into their own function: # - getUserInput( whichData, shape ) -> called from square( ) and rectangle( ) to get and validate # side, width or height from user # - areaOfParallelogram( base, height ) -> called from square( ) and rectangle( ) to compute their area # since square and rectangle are both parallelograms and therefore use the same area equation. # - displayResult( theShape, area ) -> called from the main part of the program to display the result # since all shapes will have a resulting area to display. # Set variables exitProgram = 'X' def printMenu( ) : """ Print description of program and menu displaying selection of shapes. Parameters: none. Returned value: none. """ print("Welcome to my Area Calculator.\n") print("""This AreaCalculator allows us to compute the area of:\n \ta triangle\tEnter T \ta circle\tEnter C \ta rectangle\tEnter R \ta square\tEnter S \tan ellipse\tEnter E \tto exit\t\tEnter X""") return def getSelection( ) : """ Print input instruction to user, read user input (selection) then validate user input. Parameter: "notASelectionCode" - a letter indicating that the user has entered an invalid input. Returned value: "selectedShape" - either contains a valid selection or the value of parameter "notASelectionCode". """ # Print input instruction to user and read user input selectedShape = input("\nPlease, enter your selection: ") # Validate input # Transform the user's input into an upper case letter in case # the user has entered a lower case letter as an selection selectedShape = selectedShape.upper( ) return selectedShape def getUserInput( whichData, shape ) : """ Get user input needed to calculate the area of a shape and validate it. Parameter: "whichData" - a string (either "width", "height", "side" or "radius"). "shape" - a string representing the shape selected by the user. Returned value: "returnedValue" - either contains a valid user input or 0 if the user entered either a letter or nothing. """ returnedValue = 0 theInput = input("\nPlease, enter the {0} of the {1} as a positive integer number: ".format(whichData, shape)) # Make sure the user has entered positive integer number (valid input) if not theInput.isalpha() and int(theInput) > 0: returnedValue = int(theInput) else: print("\nThe {0} you entered for the {1} ({2}) is invalid.\n".format(whichData, shape, theInput)) return returnedValue def areaOfParallelogram( base, height ): """ Returns the area of a parallelogram (generalization). """ area = base * height return area def rectangle(): """ Deals with the area computation of a rectangle. """ print("You have selected Rectangle.") # Ask for the width and height width = getUserInput( "width", "rectangle" ) if width != 0 : height = getUserInput( "height", "rectangle" ) if height != 0 : # Compute area = width * height area = areaOfParallelogram(int(width), int(height)) return area def square(): """ Deals with the area computation of a square. """ print("You have selected Square.") # Ask for the side side = getUserInput( "side", "square" ) if side != 0 : # Compute area = side * side area = areaOfParallelogram(int(side), int(side)) return area def displayResult( theShape, area ): """ Display the result, i.e., the shape and the area. Parameters: "theSshape" - a string representing the shape selected by the user "area" is its computed area or 0 if seomthing wrong happened. Returned value: none. """ if area > 0 : # Print the shape and the area print("\nThe area of the {} is {}.\n".format(theShape, area)) return # Main part of the program - top level (of execution) # Print description of program and menu displaying selection of shapes printMenu( ) # Get selection from user (+ validate input) selectedShape = getSelection( ) # As long as the user enters a valid selection ... while selectedShape != exitProgram: area = 0 # If "triangle" is selected? if selectedShape == "T": theShape = "triangle" print(theShape) # If "circle" is selected? elif selectedShape == "C": theShape = "circle" print(theShape) # If "rectangle" is selected? elif selectedShape == "R": theShape = "rectangle" area = rectangle() # If "square" is selected? elif selectedShape == "S": theShape = "square" area = square() # If "ellipse" is selected? elif selectedShape == "E" : theShape = "ellipse" print(theShape) elif len(selectedShape) == 0 : print("You have not entered anything. Please, try again!") theShape = "" # Has the user entered valid input (1 letter either T, C, R, S or E) elif not (selectedShape in ["T", "C", "R", "S", "E"] ) : print("You have not entered valid input. Please, try again!") theShape = "" # Print the shape and the area if theShape != "": displayResult( theShape, area ) # Get the next selection selectedShape = getSelection( ) # For testing purposes: # print("Exiting Main: selectedShape = ", selectedShape) print("---") # NOT YET IMPLEMENTED # Based on selection selected by user, get appropriate input from user (validate input) # If "triangle" is selected, then ask for the base and height # If "circle" is selected, then ask for the radius # If "elipse" is selected, then ask for both radii # Validate input # Compute desired area # If "triangle" is selected, then compute area = 0.5 ( base * height ) # If "circle" is selected, then compute area = pi * radius squared # If "elipse" is selected, then compute area = pi * radius1 * radius2