Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 10 - Lists - Programming Exercises - Page 354: 10.23

Answer

code

Work Step by Step

# 10.23 (Algebra: solve quadratic equations) Write a function for solving a quadratic # equation using the following header: # def solveQuadratic(eqn, roots): # The coefficients of a quadratic equation ax^2+ bx+c =0 are passed to the list # eqn and the noncomplex roots are stored in roots. The function returns the # number of roots. See Programming Exercise 4.1 on how to solve a quadratic # equation. # Write a program that prompts the user to enter values for a, b, and c and displays # the number of roots and all noncomplex roots. import math def solveQuadratic(eqn, roots): a = eqn[0] b = eqn[1] c = eqn[2] discriminant = math.pow(b, 2) - 4 * a * c if discriminant > 0: r1 = (-b + math.sqrt(b ** 2 - 4 * a * c)) / (2 * a) r2 = (-b - math.sqrt(b ** 2 - 4 * a * c)) / (2 * a) roots.append(r1) roots.append(r2) elif discriminant == 0: r1 = (-b + math.sqrt(b ** 2 - 4 * a * c)) / (2 * a) roots.append(r1) return len(roots) a, b, c = eval(input("Enter a, b, c: ")) eqn = [a, b, c] roots = [] n = solveQuadratic(eqn, roots) print("The number of roots:", n) print("The roots:", roots)
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.