Home Test Prep Challenges Create Task Project Plans About Me

Create Task

Link to Code

Link to Video

  1. a) The purpose of the program is to provide an easy-to-use calculator system that performs math functions of varying difficulty. The calculator is for students who would like to solve math problems involving fairly complex functions but either do not have access to or do not know how to correctly use a graphing calculator. The program takes an input from the user, which serves as their menu selection. If the user enters a non-integer or a number that is not between 0 and 3 inclusive, the program will catch the error and notify the user. Based on their selection, the program calls the necessary procedure. I used drivers to simplify the names of the procedures called. All of the math functions can be called by the driver() procedure, which is different in each math file. The program then prompts the user to input the necessary values for the math function that they selected. The input is the text that the user types in the menu and for the math functions. The output is displaying the prompts for the menu option that the user selected (hypotenuse, derivative, or distance) as well as printing the correct answer for the math functions.

  2. b) The name of the list is main_menu. The list contains the options for the menu. This includes the displayed text paired with the procedure for each math function. The variable menu_list returns a shallow copy of the list, which stores the copy of the original object and points the references to the objects. In other words, the shallow copy does not create a copy of the nested objects, instead it just copies the reference of nested objects. The build menu then uses the variable menu_list, which is a shallow copy of the main_menu list, as a parameter. Using a for loop that iterates through every row in menu_list, I built a dictionary from menu_list that makes it easier to print the menu and call the correct procedure based on the user’s menu selection. The list allows each menu item string to be printed dynamically instead of having to print constant values. Furthermore, to print the list, there is only one print statement and a for loop. If there was no list for the menu, each row in the menu dictionary would have to be printed separately, resulting in more lines of code and making it more difficult to add/remove items from the list. The list makes the algorithm more scalable and reusable.

  3. c) The name of the procedure is buildMenu(). It returns text printed in the terminal; therefore, the return type is none. The parameters for the procedure are title and menu_list. Title is the defined variable “title” that prints the title and menu banner. The second parameter, menu_list, is a shallow copy of the main_menu list. Once called, the algorithm goes through a sequence of events. First, the procedure prints the menu banner. Second, the procedure builds a dictionary from menu_list using an iterative for loop. Third, the menu, which is in the form of a dictionary, is printed using a for loop that iterates through key-value pairs in the dictionary. Fourth, the procedure prompts the user for an input. If the user types something other than an integer, the try-except urges them to enter an integer input. The while loop ensures that the procedure does not progress until the user enters an integer input. Fifth, the algorithm uses selection to produce specific outcomes based on the user’s input. If the user enters 0, the buildMenu() procedure returns nothing. If the user inputs an integer that is less than 0 or greater than 3, the algorithm prompts them to enter a different integer that is in the correct range. Otherwise, if the user enters an integer between 0 and 3 inclusive, buildMenu() returns the value of the item with the specified key using the Python get() function. Lastly, the buildMenu() procedure calls itself (recursion) to ensure that the menu keeps prompting the user until they choose to exit.

  4. d) The buildMenu() procedure is called in the menu() procedure. The parameters passed are title and menu_list. Within the build_Menu() procedure, the get() procedure which returns the value of the item with the specified key, takes in the argument choice. Choice is the user’s menu selection. When the program is initially run, the menu() procedure is called. The menu() procedure calls the buildMenu() procedure passing in the arguments title and menu_list. This first call prints the menu. Following the first call, the buildMenu() procedure is called recursively, and every recursive call has a different input from the user. In the video, the user inputted the values 1, 2, 3, a, and 5 in that order. Each of these arguments, when passed in the get() procedure as the choice parameter, led to a unique math function, displayed an error message, or stopped the menu. For example, the argument “2” resulted in the user being prompted to enter an expression to derive. The argument “a” led to a ValueError, and printed the message “Invalid input. Please enter an integer input.”