Question: THIS IS PYTHON CODE PROBLEM PLEASE HELP. I AM RECEIVING THE WRONG OUTPUTS. I AM SUPPOSE TO GET ANSERS THAT IS LISTED IN THE EXAMPLE
THIS IS PYTHON CODE PROBLEM PLEASE HELP. I AM RECEIVING THE WRONG OUTPUTS. I AM SUPPOSE TO GET ANSERS THAT IS LISTED IN THE EXAMPLE






THIS IS MY OUTPUT HOWEVER I AM NOT RECIEVING THE PROPER OUTPUTS LISTED IN THE EXAMPLES ABOVE. PLEASE HELP def findNextOpr(s): # s must be a nonempty string. # In this exercise +, -, *, / are all the 4 operators # The function returns -1 if there is no operator in s, # otherwise returns the position of the leftmost operator # --- code the rest of the function ----# if len(s) len(expr) or not isinstance(pos, int): print("type mismatch error: getNextNumber") return None, None, "type mismatch error: getNextNumber" else: next_num = None next_opr = None nex_opr_pos = None for i in range(len(expr)): if expr[i] != ' ': inc = ind_val = i if ((expr[i] == "+" or expr[i] == "-" or expr[i] == "*" or expr[i] == "/") and next_opr == None): next_opr = expr[i] nex_opr_pos = inc + 1 break next_num = expr[:ind_val + 1] return next_num, next_opr, nex_opr_pos # --- function code ends ---# def exeOpr(num1, opr, num2): # This is a simple utility function skipping type check num1 = float(num1) num2 = float(num2) if opr == "+": return num1 + num2 elif opr == "-": return num1 - num2 elif opr == "*": return num1 * num2 elif opr == "/": return num1 / num2 else: return None def calculator(expr): # expr: nonempty string that is an arithmetic expression # the function returns the calculated result expr = expr.replace(" ", "") pos = 0 posi = 1 next_opr = 0 result = 0 if not isinstance(expr, str) or len(expr) 1: new_num, new_opr, posi = getNextNumber(expr, pos) if new_num != None and isNumber(new_num): result = exeOpr(next_num, next_opr, new_num) next_num = result next_opr = new_opr pos = posi elif isNumber(expr): result = exeOpr(next_num, next_opr, expr) print(result) calculator(" -4 +3 -2") When any function returns an error, it must be a string containing "error" Do not include test code outside any function in the upload. Remove all your testing code before uploading your f Goal: Write the function calculator(exp), where expr is a string. This function will compute the arithmetic expression given in expr. The arithmetic expression is a string of operands and operatons that may include numeric values, four arithmetic operators(,, and extra spaces. An example of such expression is "-4.75*5 2.013 7+ 2" Notes - In the starter code provided on CANVAS, there are 4 additional functions (partially written) that will help calculator(expr) to evaluate the expression. Try to understand all the variables given in the calculator(expr) code provided. Except for exeOpr, you must code the empty segments so the five functions work completely Function requirements: The function must return the computed value if expr is a correct formula, otherwise it must return an error message When any function returns a numeric value, it must be float VDo not use exec or eval function. You will not receive credit if your program uses any of the two functions anywhere The five functions provided in the starter code must work Grading Notes - calculator(expr) [60 pts]: The grading script will feed 4 randomly chosen test inputs, each for 1.5 points. One of them will be an input that should cause an error such as "4*/2+5", whose expected returned value is an error message findNextOpr(txt) [20 pts]: 2 randomly chosen test inputs checking the correct returned values isNumber(txt) [10 pt]: 2 randomly chosen test inputs checking the correct returned values. getNextNumber(expr, index) [10 pt] returned value - randomly chosen test input checking the correct When any function returns an error, it must be a string containing "error" Do not include test code outside any function in the upload. Remove all your testing code before uploading your f Goal: Write the function calculator(exp), where expr is a string. This function will compute the arithmetic expression given in expr. The arithmetic expression is a string of operands and operatons that may include numeric values, four arithmetic operators(,, and extra spaces. An example of such expression is "-4.75*5 2.013 7+ 2" Notes - In the starter code provided on CANVAS, there are 4 additional functions (partially written) that will help calculator(expr) to evaluate the expression. Try to understand all the variables given in the calculator(expr) code provided. Except for exeOpr, you must code the empty segments so the five functions work completely Function requirements: The function must return the computed value if expr is a correct formula, otherwise it must return an error message When any function returns a numeric value, it must be float VDo not use exec or eval function. You will not receive credit if your program uses any of the two functions anywhere The five functions provided in the starter code must work Grading Notes - calculator(expr) [60 pts]: The grading script will feed 4 randomly chosen test inputs, each for 1.5 points. One of them will be an input that should cause an error such as "4*/2+5", whose expected returned value is an error message findNextOpr(txt) [20 pts]: 2 randomly chosen test inputs checking the correct returned values isNumber(txt) [10 pt]: 2 randomly chosen test inputs checking the correct returned values. getNextNumber(expr, index) [10 pt] returned value - randomly chosen test input checking the correct
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
