Question
This is a python problem. I am confused on what to do. Can you please help. This is one problem. Please help. Thank You. The
This is a python problem. I am confused on what to do. Can you please help. This is one problem. Please help. Thank You. The code that is provided is below.
THIS IS THE CODE THAT IS PROVIDED:
# - Finally, complete the four functions according to the instructions ### Use the Python debugger and the unittest module to assit you to achieve the goal of the assigment ##************************************* #Name:
def findNextOpr(txt): #txt must be a nonempty string. if len(txt)
#--- function code ends -----#
def isNumber(txt): #txt must be a non-empty string #returns True if txt is convertible to float, else False if len(txt)==0 or not isinstance(txt, str): print("type error: isNumber") return "type error: isNumber" #--- continue the rest of the code here ---#
#--- function code ends ---#
def getNextNumber(expr, pos): #expr is a given arithmetic formula of type string #pos = start position in expr #1st returned value = the next number (None if N/A) #2nd returned value = the next operator (None if N/A) #3rd retruned value = the next operator position (None if N/A) if len(expr)==0 or not isinstance(expr, str) or pos=len(expr) or not isinstance(pos, int): print("type error: getNextNumber") return None, None, "type error: getNextNumber" #--- continue the rest of the code here ---#
#--- function code ends ---#
def exeOpr(num1, opr, num2): #This funtion is just an utility function. It is skipping type check if opr=="+": return num1+num2 elif opr=="-": return num1-num2 elif opr=="*": return num1*num2 elif opr=="/": return num1um2 else: return None
def calculator(expr): #expr: nonempty string that is an arithmetic expression #the fuction returns the calculated result
if len(expr)
## Think why the next 6 lines are necessary... ## Is there another way to achieve the same result? expr = expr.strip() if expr[0]!="-": newNumber, newOpr, oprPos = getNextNumber(expr, 0) else: newNumber, newOpr, oprPos = getNextNumber(expr, 1) newNumber *= -1 #####
if newNumber is None: print("input error: line B in calculator") #Line B return "input error: line B in calculator" elif newOpr is None: return newNumber elif newOpr=="+" or newOpr=="-": mode="add" addResult=newNumber #value so far in the addition mode mulResult=None #value so far in the mulplication mode elif newOpr=="*" or newOpr=="/": mode="mul" addResult=0 mulResult=newNumber pos=oprPos+1 #the new current position opr=newOpr #the new current operator #Calculation starts here. #Use the above completed functions effectively! while True: #--- continue the rest of the while loop code here ---#
#--- end of function ---#
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
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