Programming‎ > ‎Mini-Projects‎ > ‎

01 Quadratic Formula

In this project you will implement the quadratic formula in python to solve for the roots of a quadratic equation. Your program must handle both real and imaginary roots. You are not allowed to use the imaginary math libraries for your computations. Instead, your program must handle checking for imaginary roots and compute them using real numbers, and then format the output appropriately if the roots are indeed imaginary.

This assignment will require you to use a number of important programming concepts and data types, including

 These are difficult concepts to understand in some cases, so don't underestimate how long this program will take.

Specifically your program must do the following:

1. Use a function that asks the user for the coefficients a, b, and c. The function should verify that the user has entered numeric values for the coefficients using exception handling.

2. Use a function called quad_frm(a, b, c) to return real roots as a tuple such as (1.0, -1.0)

3. quad_frm(a, b, c) must return complex roots as a tuple of tuples such as ((0, 1.0),(0, -1.0)).

4. Use doctests in quad_frm() to verify it is working correctly.

5. Catch and handle bad user input and other error conditions using exception handling, report error messages as appropriate, and ask the user to re-enter the data.

6. Use a function that accepts the tuples from quad_frm() and formats and prints the output for the user.

7. Print the roots as r1=, r2 = if the roots are real

8. Print the roots as r1 = + i*y.yyy, r2 = - i*y.yyy if the roots are imaginary

 The the following doctest code will make sure your quad_frm() function is working properly..

    >>> 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)
Your program must check for valid inputs for a, b, and c and catch runtime errors using exception handling.  If the user enters an improper value for a, b, or c, you must catch the error and exit the program. You may not use any of the built-in complex math functions, i.e., you must handle all the complex number manipulations yourself, using real values. 
You must add pertinent and appropriate remark statements to make your code more readable. For example
if d<0:     # check for imaginary roots
You may use the math library however to get a square-root function.  Use the following

from math import *

This allows you to use sqrt(x) .

Study Guide

C-Level Questions will require you to understand
  1. 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 
  • read longer code segments (10-15 lines) and describe the intent of the code. The goal isn't to give a line-by-line translation but the summarize the over-all task the code accomplishes and provide sample output. A sample response to a code fragment could be "this code reverses the string the user enters and prints it to the screen. If the user entered "hello world" the program would print "dlrow olleh"
  •  write a short program from scratch that meets a given requirement. A sample prompt could be "Given a string in the variable user_string, write a program that reverses the string an prints it to the screen."
Your responses to A and B level questions need to be written in syntactically correct python.

Nikhil Joshi,
Mar 7, 2019, 2:43 PM