I am writing some code to calculate the x using the abc-formula in python. It works almost except for one thing. I will first show you my code so you will bettter understand what I mean.
from fractions import Fraction
import math
import re
equation = input("eqation in the form and notation ax2 + bx + c = 0: ")
a = list(map(int, re.findall(r'(?<!x)-?\d+', equation.split('=', 1)[0].replace(' ', ''))))[0]
b = list(map(int, re.findall(r'(?<!x)-?\d+', equation.split('=', 1)[0].replace(' ', ''))))[1]
c = list(map(int, re.findall(r'(?<!x)-?\d+', equation.split('=', 1)[0].replace(' ', ''))))[2]
d = b**2-4*a*c
x1 = (-b+math.sqrt(d))/(2*a)
x2 = (-b-math.sqrt(d))/(2*a)
if x1 == x2:
if x1 > 1:
answer1 = x1 - math.trunc(x1)
print("X1 =",math.trunc(x1),Fraction(answer1).limit_denominator())
else:
print("X1 =",Fraction(x1).limit_denominator())
else:
if x2 and x1 > 1:
answer1 = x1 - math.trunc(x1)
answer2 = x2 - math.trunc(x2)
if x1.is_integer() and x2.is_integer() == True:
print("X1 =",math.trunc(x1), "v", "X2 =",math.trunc(x2)),
elif x1.is_integer() == True:
print("X1 =",math.trunc(x1), "v", "X2 =", math.trunc(x2),Fraction(answer2).limit_denominator())
elif x2.is_integer() == True:
print("X1 =",math.trunc(x1),Fraction(answer1).limit_denominator(),
"∨", "X2 =",math.trunc(x2))
else:
print("X1 =",math.trunc(x1),Fraction(answer1).limit_denominator(),
"∨", "X2 =", math.trunc(x2),Fraction(answer2).limit_denominator())
elif x1 > 1:
answer1 = x1 - math.trunc(x1)
print("X1 =",math.trunc(x1),Fraction(answer1).limit_denominator(), "∨", "X2 =",
Fraction(x2).limit_denominator())
elif x2 > 1:
answer2 = x2 - math.trunc(x2)
print("X1 =",Fraction(x1).limit_denominator(), "∨", "X2 =",
"X2 =", math.trunc(x2),Fraction(answer2).limit_denominator())
else:
print("X1 =",Fraction(x1).limit_denominator(), "∨", "X2 =",Fraction(x2).limit_denominator())
The important part for my question is just this (but maybe you need the rest for the context):
import re
equation = input("eqation in the form and notation ax2 + bx + c = 0: ")
a = list(map(int, re.findall(r'(?<!x)-?\d+', equation.split('=', 1)[0].replace(' ', ''))))[0]
b = list(map(int, re.findall(r'(?<!x)-?\d+', equation.split('=', 1)[0].replace(' ', ''))))[1]
c = list(map(int, re.findall(r'(?<!x)-?\d+', equation.split('=', 1)[0].replace(' ', ''))))[2]
When I run my code and my input is x2 + 5x - 6 = 0, I get an error:
File "e:\Python\equation.py", line 7, in <module>
c = list(map(int, re.findall(r'(?<!x)-?\d+', equation.split('=', 1)[0].replace(' ', ''))))[2]
IndexError: list index out of range
I understand why I get an error, because if I look at my input, there is nothing before the x2, so it can’t be in the list, so instead of three items in the list there are only two. That’s why my c is out of range in my list.
What must I change in order to make the variable a or b, 1 or -1, if there is nothing before the x**2 or x?
>Solution :
One way you could do it is to add 1's before the x's
Add this line right after the equation read
equation = re.sub('(\+|-|^)x', r'\g<1>1x', equation.replace(' ', ''))
This line looks for single +x or -x and replaces them with +1x or -1x