Python: Math expression parsing [closed]
-
15-07-2021 - |
Question
Python: So I am working on a program (which is a class assignment) that will take an expression such as 3/4/5 or 32432/23423/2354325 or 3425*343/254235 or 43252+34254-2435, etc(for all operators from +,-,/,*). and will solve the expression.
I CANT USE EVAL!!
I cant use higher level codes, I am limited to, at most, using string manipulators from the below website to split the string.
http://docs.python.org/2/library/stdtypes.html#typesseq
My method is to look at the expression the user enters and then use a find function to find the OPERATORS, and then use these operators and a slicing function (eg. s[0:x]). What I have is below and unfortunately it isnt working: *note that the print statements are in there for debugging purposes only. EDIT: why is x not defined when I run the program and enter an expression?
z= (input("expression:")).strip()
def finding(z):
if "/" in z:
x=z.find("/")
print("hi1")
elif "*" in z:
x=z.find("*")
print("hi2")
elif "+" in z:
x=z.find("+")
print("hi3")
elif "-" in z:
x=z.find("-")
print("hi4")
else:
print("error, not math expression")
return x
def Parsing(z,x):
x= finding(z)
qw=z.s[0:x]
print (qw)
# take the x-value from function finding(z) and use it to split
finding(z)
Parsing(z,x)
Solution
If you're just having trouble splitting the input into its parts, here's something to help you. I left it as readable as I could so that you could at least understand what it does. I'll explain any part of it if you need me to:
def parse(text):
chunks = ['']
for character in text:
if character.isdigit():
if chunks[-1].isdigit(): # If the last chunk is already a number
chunks[-1] += character # Add onto that number
else:
chunks.append(character) # Start a new number chunk
elif character in '+-/*':
chunks.append(character) # This doesn't account for `1 ++ 2`.
return chunks[1:]
Example usage:
>>> parse('123 + 123')
['123', '+', '123']
>>> parse('123 + 123 / 123 + 123')
['123', '+', '123', '/', '123', '+', '123']
I'll leave the rest up to you. If you aren't allowed to use .isdigit()
, you'll have to replace it with lower-level Python code.
OTHER TIPS
The easiest way for doing this, I think, is implementing a Shunting-yard algorithm to convert your equation in postfix notation and then executing it from left to right.
But since this is a class assignment, you should do the actual implementation yourself, I already gave you more than I should have.
Why is x not defined when I run the program and enter an expression?
x
is not in scope, you only define it in your methods, and you try to access it elsewhere.
z= (input("expression:")).strip()
def finding(z):
# ... removed your code ...
# in this method, you define x, which is local
# to the method, nothing outside this method has
# access to x
return x
def Parsing(z,x):
x= finding(z) # this is a different x that is assigned the
# return value from the 'finding' method.
qw=z.s[0:x] # I'm curious as to what is going on here.
print (qw)
# take the x-value from function finding(z) and use it to split
finding(z) # here, z is the value from the top of your code
Parsing(z,x) # here, x is not defined, which is where you get your error.
Since Parsing
is already calling finding
to get the value of x
, you don't need to pass it into Parsing
, you also don't need to call finding(z)
outside Parsing
, since you don't store the value anywhere.
def Parsing(z):
x= finding(z) # this is a different x that is assigned the
# return value from the 'finding' method.
qw=z.s[0:x] # I'm curious as to what is going on here.
print (qw)
# take the x-value from function finding(z) and use it to split
# finding(z) -- not needed
Parsing(z)