Python を使用して 1 つの変数の二次方程式を解き、常に例外をスローします (そして 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()

おすすめ

転載: blog.csdn.net/qq_41238751/article/details/109121679