I'm creating a math calculator that replaces variables with their respective values using string splicing. Once i have both sides of the equation with variables substituted for their values, whenever attempting to evaluate the expression I recieve an error. (Given the values in the code, it should print 26. ( 35 - 3(3) = 26 )
def apply_x(x, LHS):
for i in range(len(LHS.strip())):
if LHS[i] == 'x':
output = LHS[0:i] + '(' + x + ')' + LHS[i+1:len(LHS)]
for i in range(len(output.strip())):
if output[i] == 'x':
output = output[0:i] + '(' + x + ')' + output[i+1:len(output)]
return str(output)
equation = '35 - 3x = -8x - 5(5 - 5x)'
index = equation.find('=')
LHS = equation[0:index] # Left Hand Side
RHS = equation[index + 1:len(equation)] # Right Hand Side
x = 3
LHS_int = apply_x(str(x), LHS)
RHS_int = apply_x(str(x), RHS)
print('Original: '+equation)
print('Subbed: ' + LHS_int + '=' + RHS_int)
print('\nLHS Original: ' + str(LHS.strip()))
print('LHS Subbed : ' + LHS_int.strip())
print('RHS Original: ' + str(RHS.strip()))
print('RHS Subbed: ' + RHS_int.strip())
'''
I want the code to solve each side and check to see if
The Equation is true/false .
'''