3.2 Python Functions
Example: Given , and the quadratic equation . write a function to solve such equations. See answer in the code-cell below.
def quadra(a,b,c):
a,b,c = float(a),float(b),float(c) # three lines in one
if b**2-4*a*c < 0:
return print("No real solutions")
elif b**2-4*a*c == 0:
x1 = -b/(2*a)
return print("the solution is", x1)
else:
x1 = (-b-(b**2-4*a*c)**.5)/(2*a)
x2 = (-b+(b**2-4*a*c)**.5)/(2*a)
return print("the solutions are: ",x1," and ",x2)
Lab 3.2.1
Use the quadra(a,b,c) function above to solve .
Lab 3.2.2
Use the quadra(a,b,c) function to solve .
Adapted from Python for Introductory Statistics: Student's Lab Workbook, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.