10-26-2012, 12:41 PM
Hi, im new to the program and i'm trying to create a function can solves a string input.
For example, user input: "1-5".
My program:
will return as wanted -4, however if i type for example:
"1-5-1"
my program would return -3.
my problem is that the last expression is calculated first, so from "1-5-1" i would get 1-(5-1) = -3, how do i fix this to make it correct?
For example, user input: "1-5".
My program:
PHP Code:
def subtraction(n):
if n.find('-')>0:
a, b = n.split('-', 1)
return subtraction(b)-subtraction(a)
else:
return float(n)
"1-5-1"
my program would return -3.
my problem is that the last expression is calculated first, so from "1-5-1" i would get 1-(5-1) = -3, how do i fix this to make it correct?