1 of 25

Quadratic Equation Lab

2 of 25

Write a Python Function

to compute the roots

of a quadratic equation

Do ALL 9 steps.

at steps 3 & 9.

The purpose of this lab is not just to write a quadratic function but also to learn some of the things you can do when you write any function.

3 of 25

Given a quadratic equation of the form

the two roots can be found by using the Quadratic Equation

NOTE: To take the square root of a quantity use the Python math library sqrt() function.

4 of 25

Write a function called quad( ) that calculates the two roots of a quadratic equation.

root #1

root #2

5 of 25

What are the inputs/parameters of the quad( ) function?

a, b, and c

What are the outputs/returned values of the quad( ) function?

x1 & x2

x1

x2

6 of 25

The function uses the 3 coefficients of a quadratic equation to calculate and returns the 2 roots of the quadratic equation

Write a function that has 3 parameters and returns 2 values.

What we need to do

7 of 25

  • DEFINE your quad function in your IDE

  • define the function with the three input parameters a, b, c
  • calculate the two roots and store them in the variables root1 & root1. For now assume the number you are taking the square root of is always going to be positive.
  • for now just print the roots in your function

print(root1, root2)

8 of 25

  • Run -> Run Module or hit F5

You should see the Python Shell ...

Nothing has happened yet since all you've done so far is define the function.

However, the Python Shell now knows about your quad() function, so you can now test it by typing directly after the >>>

9 of 25

  • Test your function using the Python Shell as shown below

Show THIS EXACT screen to me

Do you know why you are getting this error?

Hint: what is the value of the discriminant? Why is that causing an error?

There should NO parentheses around these values.

10 of 25

You now have defined & tested your function.

  • Currently the function prints the roots and therefore the function is specific since it can only be used if you actually want to print the roots and print them in this one particular way

  • A more general version of this function would simply return the roots as numbers and let the user of the function decide what to do with the roots.

11 of 25

  • Generalize your function by returning the roots instead of printing the roots.

Change the print statement into a return statement - don't forget to remove the ( ) as they are not required for a return.

If necessary, refer back to this slide to see how to a function can return more than one value.

12 of 25

  • DOCUMENT your function

Add comments right before the def line of the function

  • you can cut and paste the comments from below.

Provide documentation on

  • what the function does
  • what type of inputs the function expects
  • what type of results it returns

# Computes the two roots of a quadratic

# equation given the three coefficients

# INPUT parameters

# a: quadratic coefficient (float or int)

# b: linear coefficient (float or int)

# c: constant (float or int)

# returns: root1, root2 (both floats)

13 of 25

The function is now DONE !!!

    • it has been named
    • it has been written
    • it has been tested
    • it has been made "general"
    • it has been documented

We are now ready to use or

invoke, call, execute, run

the function

14 of 25

When using the function all we need to know is

  • the function name quad
  • that it has 3 parameters each of which is a number (integer or floating point)
  • that it returns two values each of which is a number.

def

quad( )

#1,#2,#3

:

#############

return #1,#2

15 of 25

Next we need . . .

3 values that our program can use as arguments when calling the function

2 variables to store the numbers returned by the function.

16 of 25

  • Obtain the 3 values that our program can use as arguments when calling the function

Below your function put one blank line and then put the following code (indented all the way to the left, so it is not part of the function), which allows the user enter all 3 values we need.

aString = input("Enter 'a', the quadratic coefficient: ")

qcoef = float(aString)

bString = input("Enter 'b', the linear coefficient: ")

lcoef = float(bString)

cString = input("Enter 'c', the constant: ")

const = float(cString)

17 of 25

In this code the string that is returned by the input() function is used as the input to the float() function and then the value returned by float() is assigned to the variable.

A shorter way to write this code is

qcoef = float(input("Enter 'a', the quadratic coefficient: "))

lcoef = float(input("Enter 'b', the linear coefficient: "))

const = float(input("Enter 'c', the constant: "))

18 of 25

  • use / call / invoke / execute / run your quad() function with the three variables qcoef, lcoef, and const that contain the values you want to use as the arguments of the function.

Store the two values (i.e. roots) returned by the quad() function in the variables first and

second

If necessary, refer back to this slide to see how you can store multiple values returned by a function.

19 of 25

  • Now that the two roots are safely stored in the first and second variables print out these variables preceded by the text

The roots of the equation are

20 of 25

  • Run your program and enter 1, 5, and 6 for the three coefficients

show THIS EXACT screen to me

21 of 25

22 of 25

Make your function also handle imaginary roots as well as single (sometimes called repeated) roots.

23 of 25

Make your function also handle imaginary roots

  • all calculations must be done within the quad() function
  • round your numbers to 2 decimal places
  • your quad() function must return at most 3 things - where at least 2 of the 3 are numbers.1
  • your function should only have 1 return statement (at the very end of the function)

1 If your function returns a string it is not being very nice to the user of the function that may not want to print in that way (or even print anything at all, but just wants the numbers).

24 of 25

Make your function also handles quadratics that have only ONE root

25 of 25