Use python to solve the quadratic equation of one variable, always throwing an abnormal content (and use try...except...)

注意输入三个数a,b,c的语法
a,b,c=map(int,input('输入a,b,c空格隔开:').split())  
import math
def  main():
    print("This program finds the real solution to a quadratic\n")
    a,b,c=map(int,input('输入a,b,c空格隔开:').split())

    try:
       
        discRoot=math.sqrt(b*b-4*a*c)
        root1=(-b+discRoot)/(2*a)
        root2=(-b-discRoot)/(2*a)
        print ("the solutions are:",root1,root2)
    except ValueError:
        print ("No real roots")
main()

Guess you like

Origin blog.csdn.net/qq_41238751/article/details/109121679
Recommended