# AreaCalculator_3_1171.py - version 3 # # 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" # ( For 1) and 2) , see version 1. ) # 3) We continue incrementally implementing the steps of this algorithm: (Step 4) # In this version of our AreaCalculator, we add the code that deal with computing the area of a "square". # NOTE: This code does not correlate with exactly 1 specific step of our low-level algorithm, as in version 1, # but span across a few steps of our algorithm, namely: # # if "square" is selected, then ask for one side # # If "square" is selected, then compute area = side squared # # Display result # # Print the shape, the input data and the area # # 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 \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 rectangle( ): """ Get width and height of rectangle from user, then compute and display the area of such rectangle. Parameter: none. Returned value: none. """ # Echo selection to user print("You have selected 'rectangle'.") # Ask for the width and height width = input("\nPlease, enter the width of the rectangle as a positive integer number: ") # Make sure the user has entered positive integer number (valid input) if not width.isalpha() and int(width) > 0: height = input("Please, enter the height of the rectangle as a positive integer number: ") if not height.isalpha() and int(height) > 0: # Compute area = width * height width = int(width) height = int(height) area = width * height # Print the shape, the input data and the area print("\nThe area of the rectangle with a width of %d and a height of %d is %d.\n" %(width, height, area)) else: print("\nThe height you entered for the rectangle (%s) is invalid.\n" %height) else: print("\nThe width you entered for the rectangle (%s) is invalid.\n" %width) return def square( ): """ Get side of square from user, then compute and display the area of such square. Parameter: none. Returned value: none. """ # Echo selection to user print("You have selected 'square'.") # Ask for the length of the side of the square side = input("\nPlease, enter the length of the side of the square as a positive integer number: ") # Make sure the user has entered positive integer number (valid input) if not side.isalpha() and int(side) > 0: # Compute area = side ** 2 side = int(side) area = side ** 2 # Print the shape, the input data and the area print("\nThe area of the square with a side length of %d is %d.\n" %(side, area)) else: print("\nThe side you entered for the square (%s) is invalid.\n" %side) 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: # 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": rectangle() # If "square" is selected? elif selectedShape == "S": 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 "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 # Display result # Print the shape, the input data and the area