Task 3 - Creating Functions#

In your projects, you may need to create your own functions to do some complex calculations (beyond mean, median etc…). In this question you will write a function to find the roots of a quadratic equation.

To refresh your memory from high school math, a quadratic function looks like this:

../../../_images/quadratic.png

Given the standard form of a quadratic equation, \(ax^2 + bx + c = 0\) where a, b, c are known real numbers and \(a\neq0\), the quadratic formula to find unknown \(x\) is: $\(x=\frac{-b ± \sqrt{b^2-4ac}}{2a}\)$

Write a python function that calculates and prints all possible real (i.e. non-complex) answers for \(x\).

Check your function by running it on some test data points:

  • solution(1,1,1),

  • solution(1,0,-4),

  • solution(1,2,1).

Sample Output#

–> For a=1, b=1, and c=1:
The equation does not have any real solution
–> For a=1, b=0, and c=-4:
x1 = 2.0 and x2 = -2.0
–> For a=1, b=2, and c=1:
x = -1.0

#add your import statements here
def solution(a, b, c):
    # Your solution here
  Cell In[2], line 2
    # Your solution here
                        ^
SyntaxError: incomplete input
solution(1,1,1)
solution(1,0,-4)
solution(1,2,1)