ghaffarmumtat123 commited on
Commit
f5e1d69
·
verified ·
1 Parent(s): 8165c5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -4
app.py CHANGED
@@ -8,14 +8,24 @@ def solve_equation(equation):
8
  if "=" not in equation:
9
  return "Invalid input. Please enter an equation with '=' (e.g., 2*x + 3 = 5)."
10
 
11
- # Replace '^' with '**' for exponentiation
12
  equation = equation.replace("^", "**")
13
 
 
 
 
 
14
  lhs, rhs = equation.split("=")
15
- lhs = sp.sympify(lhs)
16
- rhs = sp.sympify(rhs)
17
- solution = sp.solve(lhs - rhs)
 
 
 
 
 
18
  return f"Solution: {solution}"
 
19
  except Exception as e:
20
  return f"Error: {e}"
21
 
 
8
  if "=" not in equation:
9
  return "Invalid input. Please enter an equation with '=' (e.g., 2*x + 3 = 5)."
10
 
11
+ # Replace '^' with '**' for exponentiation (common in math inputs)
12
  equation = equation.replace("^", "**")
13
 
14
+ # Remove unwanted spaces and handle possible unexpected formatting
15
+ equation = equation.replace(" ", "")
16
+
17
+ # Split the equation at the equal sign
18
  lhs, rhs = equation.split("=")
19
+
20
+ # Use sympy to parse the left and right hand sides of the equation
21
+ lhs_expr = sp.sympify(lhs)
22
+ rhs_expr = sp.sympify(rhs)
23
+
24
+ # Solve the equation lhs = rhs
25
+ solution = sp.solve(lhs_expr - rhs_expr)
26
+
27
  return f"Solution: {solution}"
28
+
29
  except Exception as e:
30
  return f"Error: {e}"
31