Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.

QUESTION

(Python)Ok so I have to make a code to create def calculator(expr): after I complete following defintions def findNextOpr(txt): def...

P.S instruction is given below too, after my code.

< I JUST NEED HELP FOR THE LAST PART >

Codes that I completed are given below

def findNextOpr(txt):

 if len(txt)<= 0 or not isinstance(txt,str):   

  return "type error: findNextOpr"

 operators = "+-*/"

 num = "' ' 0123456789"

 x = 0

 while len(txt) > 0:

  if txt[x:x+1] in operators:

   return len(txt[0:x])

  if x == len(txt) - 1:

   return -1

  x += 1

def isNumber(txt):                    

 if not isinstance(txt, str):

  return "type error: isNumber"

 if len(txt)==0:

  return False

 if len(txt) > 0:

  try:

   float(txt)

   return True

  except:

   ValueError

   return False

def getNextNumber(expr, pos):    

 '''if len(expr)==0 or not isinstance(expr, str) or pos<0 or

  pos>=len(expr) or not isinstance(pos, int):

 return None, None, "type error: getNextNumber"'''

 operators = "+-*/"

 num = "0123456789"

 space = " "

 ans1 = ""

 x = 0

 y = 0

 txt = expr[pos:len(expr)]

 i = pos

 while i < len(expr):

  if expr[i] in operators: 

   break

  elif expr[i] == space:

   if ans1 == "":

    j = i

    while ans1 == "":

     if expr[j] in operators:

      break

     if expr[j] in num:

      ans1 += expr[j]

     j += 1

    i = j

   else:

    break

  elif expr[i] in num or expr[i] == ".":

   ans1 += expr[i]

   i += 1

 if isNumber(ans1) is True:

  if "." not in ans1:

   ans1 += ".0"

 if ans1 == "":

  ans1 = None

 if findNextOpr(txt) == -1:

  ans2 = None

  ans3 = None

 else:

  ans2 = expr[pos + findNextOpr(txt)]

  ans3 = pos + findNextOpr(txt)

 return ans1, ans2, ans3

def exeOpr(num1, opr, num2):

 if opr=="+":

  return num1+num2

 elif opr=="-":

  return num1-num2

 elif opr=="*":

  return num1*num2

 elif opr=="/":

  return num1/num2

 else:

  return "error in exeOpr"

-------------------------------------- instruction--------------------------------------

def findNextOpr(txt):

"""

Takes a string and returns -1 if there is no operator in txt,

otherwise returns

the position of the leftmost operator. +, -, *, / are all the 4

operators

>>> findNextOpr('  3*   4 - 5')

3

>>> findNextOpr('8   4 - 5')

6

>>> findNextOpr('89 4 5')

-  1

"""

if len(txt)<=0 or not isinstance(txt,str):

return "type error: findNextOpr"

# --- YOU CODE STARTS HERE

# ---  CODE ENDS HERE

def isNumber(txt):

"""

Takes a string and returns True if txt is convertible to float,

False otherwise

>>> isNumber('1   2 3')

False

>>> isNumber('-  156.3')

False

>>> isNumber('29.99999999')

True

>>> isNumber('    5.9999 ')

True

"""

if not isinstance(txt, str):

return "type error: isNumber"

if len(txt)==0:

return False

# --- YOU CODE STARTS HERE

# ---  CODE ENDS HERE

def getNextNumber(expr, pos):

"""

expr is a given arithmetic formula of type string

pos is the 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)

>>> getNextNumber('8  +    5    -2',0)

(8.0, '+', 3)

>>> getNextNumber('8  +    5    -2',4)

(5.0, '-', 13)

>>> getNextNumber('4.5 + 3.15         /   5',0)

(4.5, '+', 4)

>>> getNextNumber('4.5 + 3.15         /   5',10)

(None, '/', 19)

"""

if len(expr)==0 or not isinstance(expr, str) or pos<0 or

pos>=len(expr) or not isinstance(pos, int):

return None, None, "type error: getNextNumber"

# --- YOU CODE STARTS HERE

# ---  CODE ENDS HERE

def exeOpr(num1, opr, num2):

#This function is just an utility function for calculator(expr). It is

skipping type check

if opr=="+":

return num1+num2

elif opr=="-":

return num1-num2

elif opr=="*":

return num1*num2

elif opr=="/":

return num1/num2

else:

return "error in exeOpr"

def calculator(expr):

"""

Takes a string and returns the calculated result if the

arithmethic expression is value,

and error message otherwise

>>> calculator("   -4 +3 -2")

-  3.0

>>> calculator("-4 +3 -2 / 2")

-  2.0

>>> calculator("-4 +3   - 8 / 2")

-  5.0

>>> calculator("   -4 +    3   - 8 / 2")

-  5.0

>>> calculator("23 / 12 - 223 + 5.25 * 4 * 3423")

71661.91666666667

>>> calculator("2 - 3*4")

-  10.0

>>> calculator("4++ 3 +2")

'error message'

>>> calculator("4 3 +2")

'input error line B: calculator'

"""

if len(expr)<=0 or not isinstance(expr,str): #Line A

return "input error line A: calculator"

# Concatenate '0' at he beginning of the expression if it starts with

a negative number to get '-' when calling getNextNumber

# "-2.0 + 3 * 4.0 â

becomes "0-2.0 + 3 * 4.0 â

.

expr=expr.strip()

if expr[0]=="-":

expr = "0 " + expr

newNumber, newOpr, oprPos = getNextNumber(expr, 0)

# Initialization. Holding two modes for operator precedence:

"addition" and "multiplication"

if newNumber is None: #Line B

return "input error line B: calculator"

elif newOpr is None:

return newNumber

elif newOpr=="+" or newOpr=="-":

mode="add"

addResult=newNumber     #value so far in the addition mode

elif newOpr=="*" or newOpr=="/":

mode="mul"

addResult=0

mulResult=newNumber     #value so far in the mulplication mode

addLastOpr = "+"

pos=oprPos+1                #the new current position

opr=newOpr                  #the new current operator

#Calculation starts here, get next number-operator and perform case

analysis. Conpute values using exeOpr

while True:

# --- YOU CODE STARTS HERE

# ---  CODE ENDS HERE

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question