Question: In Python - Modify the postfix eval algorithm code below so it handles errors. When an error is detected, throw an Exception with a message

In Python - Modify the postfix eval algorithm code below so it handles errors. When an error is detected, throw an Exception with a message that describes the error. (You don't have to handle these exceptions; just let them terminate the program.) Then add your own code in main() so when your program runs, the function is called suing 3 different examples with different kinds of errors.

import stack def postfix_eval(postfix_expr): operand_stack = stack.Stack() token_list = postfix_expr.split() for token in token_list: if token in "0123456789": operand_stack.push(int(token)) elif token in "+-*/": # changed to check operand2 = operand_stack.pop() operand1 = operand_stack.pop() result = do_math(token, operand1, operand2) operand_stack.push(result) return operand_stack.pop() def do_math(op, op1, op2): if op == "*": return op1 * op2 elif op == "/": return op1 / op2 elif op == "+": return op1 + op2 else: return op1 - op2 print(postfix_eval("7 8 + 3 2 + /")) def main(): 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!