Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Directions for Lab: quadratic.py Follow the directions at PyCharm Debugging. We have provided starter code at quadratic.py ; use this instead of the Solver class

Directions for Lab: quadratic.py

Follow the directions at PyCharm Debugging. We have provided starter code at quadratic.py; use this instead of the Solver class version on the PyCharm Debugging webpage. Read over the PyCharm Debugging webpage, and demonstrate that you can step through the code and view the d variable prior to the square root function call. Explain the solve() code to your duck, and determine what changes are required to fix the code. Modify code as needed, and submit the code to zyLab.

My code FOR quadratic.py :

NOTE: only one error.

test_solver 3

Test feedback

The quadratic solver fails with two solutions

PLEASE FIX BELOW CODE! THANKS!

import math # 1 def solve(a, b, c): """ Calculate solution to quadratic equation and return @param coefficients a,b,class @return either 2 roots, 1 root, or None """ if a == 0: if b == 0: if c == 0: # All coefficients are zero, so the equation has infinite solutions return None else: # Only the constant term is nonzero, so the equation has no solutions return None else: # The equation reduces to a linear equation of the form bx + c = 0 root = -c / b return root, None else: d = b ** 2 - 4 * a * c if d < 0: # The discriminant is negative, so the equation has no real solutions return None else: disc = math.sqrt(d) root1 = (-b + disc) / (2 * a) root2 = (-b - disc) / (2 * a) if d == 0: # The discriminant is zero, so the equation has a single repeated root return root1, None else: # The equation has two distinct real roots return root1, root2 if __name__ == '__main__': # This will work with given code # x^2+3x + 2 =0 results = solve(1,3,2) print("Result of x^2+2x + 3 =0: "+str(results)) # This does not work with given code # x^2+2x + 3 =0 results = solve(1,2,3) print("Result of x^2+2x + 3 =0: "+str(results)) while True: print() print("Input coefficients of quadratic equation: (ctrl-C to quit)") a = int(input("a: ")) b = int(input("b: ")) c = int(input("c: ")) result = solve(a, b, c) print(result)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

More Books

Students also viewed these Databases questions