I have been having problems making a quadratic calculator I'm almost there but the formula at the end is an error and I can't seem to fix it help would be great
code is below
import math
equation = []
operation_chosen = input("type of quadratic")
if operation_chosen == "standard form":
e = input("enter equation")
for i in e:
equation.append(i)
numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
operations = ['+', '-']
a = []
b = []
c = []
print(equation)
operation = []
x = 0
while 'x' in equation:
if equation[equation.index('x') + 1] == '^':
del equation[equation.index('x') + 2]
del equation[equation.index('x') + 1]
del equation[equation.index('x')]
else:
del equation[equation.index('x')]
print(equation)
while x < len(equation):
if equation[x] in operations:
operation.append(x)
x += 1
print(operation)
for i in range(operation[0]):
a += equation[i]
for i in range(operation[0], operation[1]):
b += equation[i]
for i in range(operation[1], len(equation)):
c += equation[i]
while '+' in b:
del b[b.index('+')]
while '-' in b:
del b[b.index('-')]
while '+' in c:
del c[c.index('+')]
while '-' in c:
del c[c.index('-')]
A = ''.join(a)
B = ''.join(b)
C = ''.join(c)
print(a)
print(b)
print(c)
print(A)
print(B)
print(C)
anser = (0-B + math.sqrt(B**-4*A*C)/2*A)
print(anser)
Can you describe what your program is supposed to do? In particular this part:
A = ''.join(a)
B = ''.join(b)
C = ''.join(c)
This does not result in A, B and C being numbers, but they get treated like numbers here:
anser = (0-B + math.sqrt(B**-4*A*C)/2*A)
And the equation used to compute anser is also incorrect. The determinant should be B**2-4*A*C. Once you compute the determinant you then have to decided if there is only one solution (determinant == 0), two real solutions (determinant > 0) or two imaginary solutions (determinant < 0)
Please provide a sample equation for testing.
the program is a quadratic calculator of standard form 10x^2+5x-10 for example the point you highlighted is to convert the a,b,c which were list to string so that they could be used in the formula
(Apr-19-2023, 11:22 PM)deanhystad Wrote: [ -> ]Can you describe what your program is supposed to do? In particular this part:
A = ''.join(a)
B = ''.join(b)
C = ''.join(c)
This does not result in A, B and C being numbers, but they get treated like numbers here:
anser = (0-B + math.sqrt(B**-4*A*C)/2*A)
And the equation used to compute answer is also incorrect. The determinant should be B**2-4*A*C. Once you compute the determinant you then have to decide if there is only one solution (determinant == 0), two real solutions (determinant > 0) or two imaginary solutions (determinant < 0)
Please provide a sample equation for testing.
I don't think this parsing code works quite right either.
for i in e:
equation.append(i)
numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
operations = ['+', '-']
a = []
b = []
c = []
print(equation)
operation = []
x = 0
while 'x' in equation:
if equation[equation.index('x') + 1] == '^':
del equation[equation.index('x') + 2]
del equation[equation.index('x') + 1]
del equation[equation.index('x')]
else:
del equation[equation.index('x')]
print(equation)
while x < len(equation):
if equation[x] in operations:
operation.append(x)
x += 1
print(operation)
for i in range(operation[0]):
a += equation[i]
for i in range(operation[0], operation[1]):
b += equation[i]
for i in range(operation[1], len(equation)):
c += equation[i]
while '+' in b:
del b[b.index('+')]
while '-' in b:
del b[b.index('-')]
while '+' in c:
del c[c.index('+')]
while '-' in c:
del c[c.index('-')]
I don't see where "-" has any affect on the results. x^2 - x -10 is evaluated the same as x^2 + x + 10
You should rethink your approach.
I reworked some of the problems by making A,B,C int but am now running into math domain errors
code
import math
equation = []
operation_chosen = input("type of quadratic")
if operation_chosen == "standard form":
e = input("enter equation")
for i in e:
equation.append(i)
numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
operations = ['+', '-']
a = []
b = []
c = []
print(equation)
operation = []
x = 0
while 'x' in equation:
if equation[equation.index('x') + 1] == '^':
del equation[equation.index('x') + 2]
del equation[equation.index('x') + 1]
del equation[equation.index('x')]
else:
del equation[equation.index('x')]
print(equation)
while x < len(equation):
if equation[x] in operations:
operation.append(x)
x += 1
print(operation)
for i in range(operation[0]):
a += equation[i]
for i in range(operation[0], operation[1]):
b += equation[i]
for i in range(operation[1], len(equation)):
c += equation[i]
while '+' in b:
del b[b.index('+')]
while '-' in b:
del b[b.index('-')]
while '+' in c:
del c[c.index('+')]
while '-' in c:
del c[c.index('-')]
A = ''.join(a)
B = ''.join(b)
C = ''.join(c)
a2 = int(A)
b2 = int(B)
b3 = math.pow(b2, 2)
c2 = int(C)
print(a)
print(b)
print(c)
print(A)
print(B)
print(C)
print(a2)
print(b2)
print(b3)
print(c2)
anser = (-b2 + math.sqrt(b3-4*a2*c2)/2*a2)
print(a)
print(b)
print(c)
print(A)
print(B)
print(C)
(Apr-20-2023, 06:55 PM)deanhystad Wrote: [ -> ]I don't think this parsing code works quite right either.
for i in e:
equation.append(i)
numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
operations = ['+', '-']
a = []
b = []
c = []
print(equation)
operation = []
x = 0
while 'x' in equation:
if equation[equation.index('x') + 1] == '^':
del equation[equation.index('x') + 2]
del equation[equation.index('x') + 1]
del equation[equation.index('x')]
else:
del equation[equation.index('x')]
print(equation)
while x < len(equation):
if equation[x] in operations:
operation.append(x)
x += 1
print(operation)
for i in range(operation[0]):
a += equation[i]
for i in range(operation[0], operation[1]):
b += equation[i]
for i in range(operation[1], len(equation)):
c += equation[i]
while '+' in b:
del b[b.index('+')]
while '-' in b:
del b[b.index('-')]
while '+' in c:
del c[c.index('+')]
while '-' in c:
del c[c.index('-')]
I don't see where "-" has any affect on the results. x^2 - x -10 is evaluated the same as x^2 + x + 10
You should rethink your approach.
Compute the determinant:
det = b**2-4*a*c
If this is zero, you have one root which = -b / (2 * a) <- Notice the parentheses around (2 * a)
If it is not zero you have two roots.
det = det**0.5
roots = (-b + det) / (2 * a), (-b - det) / (2 * a)
These roots will be real or imaginary based on the sign of the determinant.
If you really think you need to turn the equation into a list of characters.
equation = list(input("enter equation ").lower())
The lower() is just in case you user likes using capitol letters. Converts "X" to "x".
I think it makes more sense to treat the equation as a string. You can check for substrings (is "x^2" in equation), split the string around a separator (a, equation = equation.split("x^2")).