Search code examples
pythonpython-idle

Showing input prompt in python


I am using IDLE for Python on a Mac OS. I wrote the following in a .py file:

import math
def main():
    print "This program finds the real solution to a quadratic"
    print

    a, b, c = input("Please enter the coefficients (a, b, c): ")

    discRoot = math.sqrt(b * b-4 * a * c)
    root1 = (-b + discRoot) / (2 * a)
    root2 = (-b - discRoot) / (2 * a)

    print
    print "The solutions are: ", root1, root2

main()

IDLE now permanently displays:

This program finds the real solution to a quadratic

Please enter the coefficients (a, b, c):

When I enter 3 numbers (ex: 1,2,3) IDLE does nothing. When I hit enter IDLE crashes (no crash report).

I quit and restarted but IDLE is now permanemtly displaying the above and won't respond to other files.


Solution

  • The math module does not support complex numbers. If you replace import math with import cmath and math.sqrt with cmath.sqrt, your script should work like a charm.

    EDIT: I just read "This program finds the real solution to a quadratic". Taking into consideration that you only want real roots, you should check for negative discriminants as the Kevin has pointed out.