Question
(continue for inClass 4, question 2) Modify a given interactive calculator python program to handle exception errors. You start by downloading a python program (calculator_no_Except.py)
(continue for inClass 4, question 2) Modify a given interactive calculator python program to handle exception errors. You start by downloading a python program (calculator_no_Except.py) from Canvas. The program should run without modification. (make sure it works, save a copy, and begin to make the change (one at a time) ) The program does not handle exception errors well. There is no "try-except" block to handle the raised errors. Your focuses are on formula-related errors. You need to write codes to handle the following three raised FormulaError: a. Number of elements for the formula (must be 3) (line 9) b. The 1st and 3rd elements must be a number (line 15) c. The operator (2nd element) must be one of (+, -, *, /) (line 29) For this interactive program, a user enters the inputs (string) in a formula format that consists of a number, an operator (eg +, -, *, /), and another number, separated by white space. For example for 3+2, user must enter 3 + 2 (not 3+2). The program has a custom error type (FormulaError(Exception)) to capture formula-related errors. (line 2 in the program) There are two provided methods, parse_input(user_input), and calculate(n1, op, n2), that could potentially raise formula-related errors. Your codes (lines 31 to 38 ) should handle the raised errors from the methods The parse_input (user_input) method uses .split( ) to separate the user_input (string) into three separate strings (aka tokens). Two potential errors (a, b ) are raised in lines 9, and 15). One possible error( c) is in the calculate(n1, op, n2) method line 29. The program calculates and prints out the result if the input is valid. Then, the program prompts the user to enter new inputs repeatably (>>>) until the user enters "quit". From your downloaded py program (calculator_no_Except.py), run the program with the following input 5 + 9 5+9 5 + + 9 a + 9 5 + t 2 r 9 Except for the first entry (5 + 9), all others crash.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started