# AreaCalculator_1.py # # 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" # 1) We write a header comment block # 2) We copy and paste the low-level algorithm into our empty program. # The steps of this algorithm become our comments. # 3) We start incrementally implementing the steps of this algorithm: (Step 4) # In this version of our AreaCalculator, we implement the first few steps of our algorithm, namely # - Print menu # - Print description of program # - Print menu displaying selection of shapes # - Get selection from user (+ validate input) # - Print input instruction to user and read user input # - Validate input # 4) We make sure our partial program executes (it has no syntax/runtime errors). (Step 5) # 5) Then, we create test plans for it and test it (make sure it has no semantic errors). (Step 5) # 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 \t* a triangle\tEnter T \t* a circle\tEnter C \t* a rectangle\tEnter R \t* a square\tEnter S \t* an ellipse\tEnter E \t* to 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 # 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 : # If "triangle" is selected? if selectedShape == "T": print("triangle") # If "circle" is selected? elif selectedShape == "C": print("circle") # If "rectangle" is selected? elif selectedShape == "R": print("rectangle") # If "square" is selected? elif selectedShape == "S": print("square") # If "ellipse" is selected? elif selectedShape == "E" : print("elipse") elif len(selectedShape) == 0 : print("You have not entered anything. Please, try again!") # 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!") 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 "rectangle" is selected, then ask for the width and height # If "square" is selected, then ask for one side # 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 "rectangle" is selected, then compute area = width * height # If "square" is selected, then compute area = side squared # If "elipse" is selected, then compute area = pi * radius1 * radius2 # Display result # Print the shape, the input data and the area