Python‎ > ‎Python Mini Projects‎ > ‎Study Guides‎ > ‎

01 Quadratics Study Guide

SAMPLE CODE


import math

def get_coeff(prompt):
    """
    function displays a prompt to the user and guarantees the returned
    value is float.

    c = the floating point value the user types in
    """
    while True:
        try:
            c = float(raw_input(prompt))
            return c
       
        except ValueError:
            print "Error! Coffecients must be numbers."

    
def quad_frm(a, b, c):
    """
    given the coefficients for a quadratic equation, returns the roots, either
    real or imaginary, as a tuple of values.

    If roots r1 and r2 are real, they are returned as the tuple (r1, r2).
    If the roots are imaginary, e.g. r + c * i and r - c * i, they are
    returned as a tuple of tuples, e.g. ( (r,c),(r,-c) )

    a, b, c = coefficients for the quadratic a * x^2 + b * x + c
    r1, r2 = the roots of the quadratic


    >>> quad_frm(1,2,1)
    (-1.0, -1.0)

    >>> quad_frm(1, 0, 1)
    ((0.0, 1.0), (0.0, -1.0))

    >>> quad_frm(1, -1, -6)
    (3.0, -2.0)
    
    >>> quad_frm(1,-4,13)
    ((2.0, 3.0), (2.0, -3.0))
    
    >>> quad_frm(2,-1,-1)
    (1.0, -0.5)
    """
    imaginary = False  # assume real roots to start
    
    d = b*b-4*a*c  # calculate discriminant
    
    if d<0:   # complex roots!
        imaginary = True
        d = abs(d)

    d = math.sqrt(d)

    if imaginary:
        r1 = -1*b / (2.0 * a), d / (2.0 * a)
        r2 = -1*b / (2.0 * a), -1*d / (2.0 * a)
    else:
        r1 = (-1*b + d) / (2.0 * a)
        r2 = (-1*b - d) / (2.0 * a)

    return r1, r2

def print_roots(r1, r2):
    """
    prints the roots of the quadratic. The roots must be a tuple of reals
    for real roots, or a tuple of tuples if imaginary.
    """
    if type(r1) == tuple:
        print "Roots are %g+%gi and %g%gi" % ( round(r1[0],3),
                                               round(r1[1],3),
                                               round(r2[0],3),
                                               round(r2[1],3) )
    else:
        print "Roots are %.4g and %.4g" % (r1, r2)
        

def main():
    while True:
        print "\nPlease enter coefficients. Set a = 0 to exit.\n"
        a = get_coeff('Please enter a number for a: ')
        if a == 0: break
        
        b = get_coeff('Please enter a number for b: ')
        c = get_coeff('Please enter a number for c: ')
        
        (r1, r2) = quad_frm(a, b, c)
        print_roots(r1, r2)

if __name__ == '__main__':
    import doctest, sys
    doctest.testmod()
    main()
    

Study Guide


C-Level Questions will require you to understand
  1. raw_input()
  2. tuples
  3. while() loops
  4. If()-else, if()-elif(), and if()-elif()-else
C-level questions only require you to understand simple code fragments and write their output. You do not need to write any code for the C-level questions.

B- and A-Level Questions will require you understand 
  1. C-Level content
  2. Error Handling ( try-except )
B- and A-level questions require you to not only understand code fragments and write their output, but also write short code fragments from scratch that meet the question's requirements.

The coding test will require you to understand B and A-level content.

The Python Tutorials page is a great place to start for your studying.  There's also the unofficial textbook, How to Think Like a Computer Scientist.