Quadratic Equation Lab
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.
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.
Write a function called quad( ) that calculates the two roots of a quadratic equation.
root #1
root #2
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
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
print(root1, root2)
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 >>>
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.
You now have defined & tested your function.
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.
Add comments right before the def line of the function
Provide documentation on
# 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)
The function is now DONE !!!
We are now ready to use or
invoke, call, execute, run
the function
When using the function all we need to know is
def
quad( )
#1,#2,#3
:
#############
return #1,#2
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.
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)
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: "))
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.
The roots of the equation are
show THIS EXACT screen to me
Make your function also handle imaginary roots as well as single (sometimes called repeated) roots.
Make your function also handle imaginary roots
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).
Make your function also handles quadratics that have only ONE root