0

I have written a program that can convert a Quadratic equation written in standard form to vertex form. I am a beginner in Python and coding in general. I have posted programs on StackOverflow before but have received a lot of criticism towards them. I am trying to improve upon my skills as a coder and I am wondering if there is any way I can shorter/simplify or just overall improve this program I have written? Thanks in advance!

'''
Standard Form to Vertex Form Converter
Created by Patrick Thomas
'''

#Preliminary Information--------------------------------------------------------
import time
time.sleep(0.5)
print('Standard Form to Vertex Form Converter')
time.sleep(0.5)

goAgain = True

#greatest common divisor funct
def gcd(num, den):
    if int(den) == 0:
        return int(num)
    return gcd(int(den), int(num) % int(den))

#While Loop---------------------------------------------------------------------
while goAgain == True:
    
    #input
    time.sleep(0.5)
    print('---------------------')
    print('ax² + bx + c')
    print('---------------------')
    
    a = int(input('a = '))
    
    b = int(input('b = '))
    
    c = int(input('c = '))
    print('')
    
    #h calculation
    two_a = 2 * a // gcd(2 * a, -b % 2 * a)
    neg_b = -b // gcd(2 * a, -b % 2 * a)
    
    #y-int calculation
    yInt = a * 0 ** 2 + b * 0 + c

    #k calculation
    bSquared = b ** 2 // gcd(b ** 2, 4 * a)
    four_a = 4 * a // gcd(b ** 2, 4 * a)
    
    #other variables
    hNumerator = neg_b
    hDenomator = two_a

    kNumerator = c * four_a - bSquared
    kDenomator = four_a

    h = neg_b
    k = c * four_a - bSquared
    hReverse = neg_b * -1
    kNeg = c * four_a - bSquared * -1
    
    #output
    if hDenomator == 1 and kDenomator == 1:
        if a == 1:
            if k >= 0:
                if hReverse >= 0:
                    print('f(x) = (x + {})² + {}'.format(int(hReverse),int(k)))
                    print('Vertex = ({},{})'.format(int(h),int(k)))
                    print('Axis of Sym = {}'.format(int(h)))
                elif hReverse < 0:
                    print('f(x) = (x - {})² + {}'.format(int(h),int(k)))
                    print('Vertex = ({},{})'.format(int(h),int(k)))
                    print('Axis of Sym = {}'.format(int(h)))
            elif k < 0:
                if hReverse >= 0:
                    print('f(x) = (x + {})² - {}'.format(int(hReverse),int(kNeg)))
                    print('Vertex = ({},{})'.format(int(h),int(k)))
                    print('Axis of Sym = {}'.format(int(h)))
                elif hReverse < 0:
                    print('f(x) = (x - {})² - {}'.format(int(h),int(kNeg)))
                    print('Vertex = ({},{})'.format(int(h),int(k)))
                    print('Axis of Sym = {}'.format(int(h)))
        elif a == -1:
            if k > 0:
                if hReverse >= 0:
                    print('f(x) = -(x + {})² + {}'.format(int(hReverse),int(k)))
                    print('Vertex = ({},{})'.format(int(h),int(k)))
                    print('Axis of Sym = {}'.format(int(h)))
                elif hReverse < 0:
                    print('f(x) = -(x - {})² + {}'.format(int(h),int(k)))
                    print('Vertex = ({},{})'.format(int(h),int(k)))
                    print('Axis of Sym = {}'.format(int(h)))
            elif k < 0:
                if hReverse >= 0:
                    print('f(x) = -(x + {})² - {}'.format(int(hReverse),int(kNeg)))
                    print('Vertex = ({},{})'.format(int(h),int(k)))
                    print('Axis of Sym = {}'.format(int(h)))
                elif hReverse < 0:
                    print('f(x) = -(x - {})² - {}'.format(int(h),int(kNeg)))
                    print('Vertex = ({},{})'.format(int(h),int(k)))
                    print('Axis of Sym = {}'.format(int(h)))
        else:
            if k >= 0:
                if hReverse >= 0:
                    print('f(x) = {}(x + {})² + {}'.format(a,int(hReverse),int(k)))
                    print('Vertex = ({},{})'.format(int(h),int(k)))
                    print('Axis of Sym = {}'.format(int(h)))
                elif hReverse < 0:
                    print('f(x) = {}(x - {})² + {}'.format(a,int(h),int(k)))
                    print('Vertex = ({},{})'.format(int(h),int(k)))
                    print('Axis of Sym = {}'.format(int(h)))
            elif k < 0:
                if hReverse >= 0:
                    print('f(x) = {}(x + {})² - {}'.format(a,int(hReverse),int(kNeg)))
                    print('Vertex = ({},{})'.format(int(h),int(k)))
                    print('Axis of Sym = {}'.format(int(h)))
                elif hReverse < 0:
                    print('f(x) = {}(x - {})² - {}'.format(a,int(h),int(kNeg)))
                    print('Vertex = ({},{})'.format(int(h),int(k)))
                    print('Axis of Sym = {}'.format(int(h)))
    
    elif hDenomator > 1 and kDenomator > 1:
        if a == 1:
            if k >= 0:
                if hReverse >= 0:
                    print('f(x) = (x + {}/{})² + {}/{}'.format(int(hReverse),int(hDenomator),int(kNumerator),int(kDenomator)))
                    print('Vertex = ({}/{},{}/{})'.format(int(hNumerator),int(hDenomator),int(kNumerator),int(kDenomator)))
                    print('Axis of Sym = {}/{}'.format(int(hNumerator),int(hDenomator)))
                elif hReverse < 0:
                    print('f(x) = (x - {}/{})² + {}/{}'.format(int(hNumerator),int(hDenomator),int(kNumerator),int(kDenomator)))
                    print('Vertex = ({}/{},{}/{})'.format(int(hNumerator),int(hDenomator),int(kNumerator),int(kDenomator)))
                    print('Axis of Sym = {}/{}'.format(int(hNumerator),int(hDenomator)))
            elif k < 0:
                if hReverse >= 0:
                    print('f(x) = (x + {}/{})² - {}/{}'.format(int(hReverse),int(hDenomator),int(kNeg),int(kDenomator)))
                    print('Vertex = ({}/{},{}/{})'.format(int(hNumerator),int(hDenomator),int(kNumerator),int(kDenomator)))
                    print('Axis of Sym = {}/{}'.format(int(hNumerator),int(hDenomator),int(kNumerator),int(kDenomator)))
                elif hReverse < 0:
                    print('f(x) = (x - {}/{})² - {}/{}'.format(int(hNumerator),int(hDenomator),int(kNeg),int(kDenomator)))
                    print('Vertex = ({}/{},{}/{})'.format(int(hNumerator),int(hDenomator),int(kNumerator),int(kDenomator)))
                    print('Axis of Sym = {}/{}'.format(int(hNumerator),int(hDenomator)))
        elif a == -1:
            if k >= 0:
                if hReverse >= 0:
                    print('f(x) = -(x + {}/{})² + {}/{}'.format(int(hReverse),int(k)))
                    print('Vertex = ({}/{},{}/{})'.format(int(h),int(k)))
                    print('Axis of Sym = {}/{}'.format(int(h)))
                elif hReverse < 0:
                    print('f(x) = -(x - {}/{})² + {}/{}'.format(int(h),int(k)))
                    print('Vertex = ({}/{},{}/{})'.format(int(h),int(k)))
                    print('Axis of Sym = {}/{}'.format(int(h)))
            elif k < 0:
                if hReverse >= 0:
                    print('f(x) = -(x + {}/{})² - {}/{}'.format(int(hReverse),int(hDenomator),int(kNeg),int))
                    print('Vertex = ({}/{},{}/{})'.format(int(hNumerator),int(hDenomator),int(kNumerator),int(kDenomator)))
                    print('Axis of Sym = {}/{}'.format(int(hNumerator),int(hDenomator)))
                elif hReverse < 0:
                    print('f(x) = -(x - {}/{})² - {}/{}'.format(int(hNumerator),int(hDenomator),int(kNeg),int(kDenomator)))
                    print('Vertex = ({}/{},{}/{})'.format(int(hNumerator),int(hDenomator),int(kNumerator),int(kDenomator)))
                    print('Axis of Sym = {}/{}'.format(int(hNumerator),int(hDenomator)))
        else:
            if k >= 0:
                if hReverse >= 0:
                    print('f(x) = {}(x + {}/{})² + {}/{}'.format(a,int(hReverse),int(hDenomator),int(kNumerator),int(kDenomator)))
                    print('Vertex = ({}/{},{}/{})'.format(int(hNumerator),int(hDenomator),int(kNumerator),int(kDenomator)))
                    print('Axis of Sym = {}/{}'.format(int(hNumerator),int(hDenomator)))
                elif hReverse < 0:
                    print('f(x) = {}(x - {}/{})² + {}/{}'.format(a,int(hNumerator),int(hDenomator),int(kNumerator),int(kDenomator)))
                    print('Vertex = ({}/{},{}/{})'.format(int(hNumerator),int(hDenomator),int(kNumerator),int(kDenomator)))
                    print('Axis of Sym = {}/{}'.format(int(hNumerator),int(hDenomator)))
            elif k < 0:
                if hReverse >= 0:
                    print('f(x) = {}(x + {}/{})² - {}/{}'.format(a,int(hReverse),int(hDenomator),int(kNeg),int(kDenomator)))
                    print('Vertex = ({}/{},{}/{})'.format(int(hNumerator),int(hDenomator),int(kNumerator),int(kDenomator)))
                    print('Axis of Sym = {}/{}'.format(int(hNumerator),int(hDenomator)))
                elif hReverse < 0:
                    print('f(x) = {}(x - {}/{})² - {}/{}'.format(a,int(hNumerator),int(hDenomator),int(kNeg),int(kDenomator)))
                    print('Vertex = ({}/{},{}/{})'.format(int(hNumerator),int(hDenomator),int(kNumerator),int(kDenomator)))
                    print('Axis of Sym = {}/{}'.format(int(hNumerator),int(hDenomator)))
    
    elif hDenomator > 1 and kDenomator == 1:
        if a == 1:
            if k >= 0:
                if hReverse >= 0:
                    print('f(x) = (x + {}/{})² + {}'.format(int(hReverse),int(hDenomator),int(k)))
                    print('Vertex = ({}/{},{})'.format(int(hNumerator),int(hDenomator),int(k)))
                    print('Axis of Sym = {}/{}'.format(int(hNumerator),int(hDenomator)))
                elif hReverse < 0:
                    print('f(x) = (x - {}/{})² + {}'.format(int(hNumerator),int(hDenomator),int(k)))
                    print('Vertex = ({}/{},{})'.format(int(hNumerator),int(hDenomator),int(k)))
                    print('Axis of Sym = {}/{}'.format(int(hNumerator),int(hDenomator)))
            elif k < 0:
                if hReverse >= 0:
                    print('f(x) = (x + {}/{})² - {}'.format(int(hReverse),int(hDenomator),int(kNeg)))
                    print('Vertex = ({}/{},{})'.format(int(hNumerator),int(hDenomator),int(k)))
                    print('Axis of Sym = {}/{}'.format(int(hNumerator),int(hDenomator)))
                elif hReverse < 0:
                    print('f(x) = (x - {}/{})² - {}'.format(int(hNumerator),int(hDenomator),int(kNeg)))
                    print('Vertex = ({}/{},{})'.format(int(hNumerator),int(hDenomator),int(k)))
                    print('Axis of Sym = {}/{}'.format(int(hNumerator),int(hDenomator)))
        elif a == -1:
            if k >= 0:
                if hReverse >= 0:
                    print('f(x) = -(x + {}/{})² + {}'.format(int(hReverse),int(hDenomator),int(k)))
                    print('Vertex = ({}/{},{})'.format(int(hNumerator),int(hDenomator),int(k)))
                    print('Axis of Sym = {}/{}'.format(int(hNumerator),hDenomator))
                elif hReverse < 0:
                    print('f(x) = -(x - {}/{})² + {}'.format(int(hNumerator),int(hDenomator),int(k)))
                    print('Vertex = ({}/{},{})'.format(int(hNumerator),int(hDenomator),int(k)))
                    print('Axis of Sym = {}/{}'.format(int(hNumerator),int(hDenomator)))
            elif k < 0:
                if hReverse >= 0:
                    print('f(x) = -(x + {}/{})² - {}'.format(int(hReverse),int(hDenomator),int(kNeg)))
                    print('Vertex = ({}/{},{})'.format(int(hNumerator),int(hDenomator),int(k)))
                    print('Axis of Sym = {}/{}'.format(int(hNumerator),int(hDenomator)))
                elif hReverse < 0:
                    print('f(x) = -(x - {}/{})² - {}'.format(int(hNumerator),int(hDenomator),int(kNeg)))
                    print('Vertex = ({}/{},{})'.format(int(hNumerator),int(hDenomator),int(k)))
                    print('Axis of Sym = {}/{}'.format(int(hNumerator),int(hDenomator)))
        else:
            if k >= 0:
                if hReverse >= 0:
                    print('f(x) = {}(x + {}/{})² + {}'.format(a,int(hReverse),int(hDenomator),int(k)))
                    print('Vertex = ({}/{},{})'.format(int(hNumerator),int(hDenomator),int(k)))
                    print('Axis of Sym = {}/{}'.format(int(hNumerator),int(hDenomator)))
                elif hReverse < 0:
                    print('f(x) = {}(x - {}/{})² + {}'.format(a,int(hNumerator),int(hDenomator),int(k)))
                    print('Vertex = ({}/{},{})'.format(int(hNumerator),int(hDenomator),int(k)))
                    print('Axis of Sym = {}/{}'.format(int(hNumerator),int(hDenomator)))
            elif k < 0:
                if hReverse >= 0:
                    print('f(x) = {}(x + {}/{})² - {}'.format(a,int(hReverse),int(hDenomator),int(kNeg)))
                    print('Vertex = ({}/{},{})'.format(int(hNumerator),int(hDenomator),int(k)))
                    print('Axis of Sym = {}/{}'.format(int(hNumerator),int(hDenomator)))
                elif hReverse < 0:
                    print('f(x) = {}(x - {}/{})² - {}'.format(a,int(hNumerator),int(hDenomator),int(kNeg)))
                    print('Vertex = ({}/{},{})'.format(int(hNumerator),int(hDenomator),int(k)))
                    print('Axis of Sym = {}/{}'.format(int(hNumerator),int(hDenomator)))
    
    elif hDenomator == 1 and kDenomator > 1:
        if a == 1:
            if k >= 0:
                if hReverse >= 0:
                    print('f(x) = (x + {})² + {}/{}'.format(int(hReverse),int(kNumerator),int(kDenomator)))
                    print('Vertex = ({},{}/{})'.format(int(h),int(kNumerator),int(kDenomator)))
                    print('Axis of Sym = {}'.format(int(h)))
                elif hReverse < 0:
                    print('f(x) = (x - {})² + {}/{}'.format(int(h),int(kNumerator),int(kDenomator)))
                    print('Vertex = ({},{}/{})'.format(int(h),int(kNumerator),int(kDenomator)))
                    print('Axis of Sym = {}'.format(int(h)))
            elif k < 0:
                if hReverse >= 0:
                    print('f(x) = (x + {})² - {}/{}'.format(int(hReverse),int(kNeg),int(kDenomator)))
                    print('Vertex = ({},{})'.format(int(h),int(kNumerator),int(kDenomator)))
                    print('Axis of Sym = {}'.format(int(h)))
                elif hReverse < 0:
                    print('f(x) = (x - {})² - {}/{}'.format(int(h),int(kNeg),int(kDenomator)))
                    print('Vertex = ({},{}/{})'.format(int(h),int(kNumerator),int(kDenomator)))
                    print('Axis of Sym = {}'.format(int(h)))
        elif a == -1:
            if k >= 0:
                if hReverse >= 0:
                    print('f(x) = -(x + {})² + {}/{}'.format(int(hReverse),int(kNumerator),int(kDenomator)))
                    print('Vertex = ({},{}/{})'.format(int(h),int(kNumerator),int(kDenomator)))
                    print('Axis of Sym = {}'.format(int(h)))
                elif hReverse < 0:
                    print('f(x) = -(x - {})² + {}/{}'.format(int(h),int(kNumerator),int(kDenomator)))
                    print('Vertex = ({},{}/{})'.format(int(h),int(kNumerator),int(kDenomator)))
                    print('Axis of Sym = {}'.format(int(h)))
            elif k < 0:
                if hReverse >= 0:
                    print('f(x) = -(x + {})² - {}/{}'.format(int(hReverse),int(kNeg)))
                    print('Vertex = ({},{}/{})'.format(int(h),int(kNumerator),int(kDenomator)))
                    print('Axis of Sym = {}'.format(int(h)))
                elif hReverse < 0:
                    print('f(x) = -(x - {})² - {}/{}'.format(int(h),int(kNeg),int(kDenomator)))
                    print('Vertex = ({},{}/{})'.format(int(h),int(kNumerator),int(kDenomator)))
                    print('Axis of Sym = {}'.format(int(h)))
        else:
            if k >= 0:
                if hReverse >= 0:
                    print('f(x) = {}(x + {})² + {}/{}'.format(a,int(hReverse),int(kNumerator),int(kDenomator)))
                    print('Vertex = ({},{}/{})'.format(int(h),int(kNumerator),int(kDenomator)))
                    print('Axis of Sym = {}'.format(int(h)))
                elif hReverse < 0:
                    print('f(x) = {}(x - {})² + {}/{}'.format(a,int(h),int(kNumerator),int(kDenomator)))
                    print('Vertex = ({},{}/{})'.format(int(h),int(kNumerator),int(kDenomator)))
                    print('Axis of Sym = {}'.format(int(h)))
            elif k < 0:
                if hReverse >= 0:
                    print('f(x) = {}(x + {})² - {}/{}'.format(a,int(hReverse),int(kNeg),int(kDenomator)))
                    print('Vertex = ({},{}/{})'.format(int(h),int(kNumerator),int(kDenomator)))
                    print('Axis of Sym = {}'.format(int(h)))
                elif hReverse < 0:
                    print('f(x) = {}(x - {})² - {}/{}'.format(a,int(h),int(kNeg),int(kDenomator)))
                    print('Vertex = ({},{}/{})'.format(int(h),int(kNumerator),int(kDenomator)))
                    print('Axis of Sym = {}'.format(int(h)))
#End of Program-----------------------------------------------------------------
Patman1O1
  • 5
  • 4
  • 2
    If the code works and you're looking for advice on improving it, [codereview.se] is the appropriate place. But see https://codereview.meta.stackexchange.com/questions/5777/a-guide-to-code-review-for-stack-overflow-users first. – Barmar Jul 10 '22 at 02:03
  • 1
    For working code that you want a review of, I'd recommend [the Code Review StackExchange](https://codereview.stackexchange.com/); it's rather more focused on review. – ShadowRanger Jul 10 '22 at 02:03
  • All your elif statements could be simple else statements, because they are just covering the remainder of possibilities from the prior if. Also, remove the duplicated lines out of the two case and place them AFTER the differences. – RufusVS Jul 10 '22 at 02:16

1 Answers1

0

Don't explicitly evaluate the boolean opposite of an expression you just evaluated. Example:

        if k >= 0:
            if hReverse >= 0:
                ...
            elif hReverse < 0:
                ...
        elif k < 0:

No point in testing if hReverse < 0 because you've already tested for hReverse >= 0. Same thing goes for k < 0 You can just use an else clause. Use comments if you need readability

This:

        if k >= 0:
            if hReverse >= 0:
                ...
            else:                  # hReverse < 0
                ...
        else:                      # k < 0

What happens if a doesn't evaluate to 1 or -1. I don't see you handling that case.

I have mixed feelings on your near-redundant print.format statements. Consider this simple reduction for your first block

if hDenomator == 1 and kDenomator == 1:
    if a == 1:
        if k >= 0:
            if hReverse >= 0:
                print('f(x) = (x + {})² + {}'.format(int(hReverse),int(k)))
            else:
                print('f(x) = (x - {})² + {}'.format(int(h),int(k)))
            print('Vertex = ({},{})'.format(int(h),int(k)))
            print('Axis of Sym = {}'.format(int(h)))
selbie
  • 100,020
  • 15
  • 103
  • 173