Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code problems
#1
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)
deanhystad write Apr-19-2023, 09:27 PM:
Please wrap code in Python tags
Reply
#2
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.
Reply
#3
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.
Reply
#4
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.
Reply
#5
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.
deanhystad write Apr-20-2023, 07:47 PM:
Please wrap code in python tags to retain proper indenting.

Use the New Reply button instead of the Reply button unless referencing something in another post. Your post should not contain other posts, just portions of other posts that are needed to understand your new post.
Reply
#6
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")).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  multiple problems with code SrijaRamarthy 2 1,870 Nov-06-2019, 06:24 AM
Last Post: SrijaRamarthy
  code problems lokchi2017 3 2,721 Jul-06-2018, 01:58 AM
Last Post: Zombie_Programming
  code editor/ IDE problems nameno1had 2 3,328 Mar-12-2018, 10:09 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020