Ok so basically I have an assignment in my first comp sci class in high school and I was able to figure out how to do most of it. We are supposed to make a calculator that can do basic functions with the PEMDAS operators and do calculations that cross over the different operators. I was able to get most of it down, but I can't figure out how to get the division, subtraction, or exponentiation to work with more than 2 numbers. Please help and thank you!
Assignment Instructions:
In the following exercise, you will be creating a program that uses recursion to solve any basic equation (addition, subtraction, multiplication, division, and exponents). Below is a framework for your program.
equation = input("Please enter an equation: ")
def solveIt(e):
# loop through e
# if you find a plus sign
# return recursive call @param part of e before the plus sign +
# recursive call @param part of e after the plus sign
return e
print(solveIt(equation))
Do not change any of the existing code. The instructions provided will help you get your code to solve equations with addition. Test your code with a compound equation like 343 + 87 + 5, and verify the correct result before you move on to the next step.
Now here is the code that I have which I can use to do 8/2, but I cannot do 8/2/2:
expression = input("Please enter an equation: ")
equation = expression.replace(' ', '')
res=0
total=0
def solveIt(e):
global res, total
for i in range(len(e)):
if e[i]=="/":
e1 = e[:i]
e2 = e[i+1:]
total=total+solveIt(e1) / solveIt(e2)
return total
return int(e)
(solveIt(equation))
print("total:",total)
Thanks for the help!