Question: In Python. Continue from question 2 to answer question 3 3. (continue for inClass 4, question 2) Modify a given interactive calculator python program to


3. (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 3ad elements must be a number (line 15) c. The operator ( 2md 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 +,, , 7 , 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+95+95++9a+9 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+95+95++9a+95+t2r9 Except for the first entry (5+9), all others crash. Results: (from your modified program) 5+9 14.0 5+9 Formular error: Input does not consist of three elements 5+ Formular error: Input does not consist of three elements >+9 Formular error: Input does not consist of three elements a+9 Formular error: The first "a" and third "9" input value must be numbers >+t Formular error: The first " 5.0 and third " t " input value must be numbers 2r9 Formular error: r is not a valid operator 2) Create a dog class with _init __ 0 without arguments The init__self) method initializes the object's state(s). Like methods, a constructor also contains a collection of statements(i.e. instructions) during object creation. The (_init_(self) ) method is the first statement when creating an object of a class (instantiation) In this class (Cat) there are two more functions (aka methods): def say_hi(self): print(self.name,'meow') def str__self): return f'cat names is \{self.name\}' \# _str_ (self) is a "special" function used to display the object attributes (i.e. name) - Create two objects (p1, p2) as (Lucy, Kitty) - Create a condition to create an object p3 and print(p3) (for example) x=5 if x=5: p3=Dog2() p3.name = "Blackie" print(p3) Result: Lucy meow Kitty meow cat name is Blackie
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
