这是我目前掌握的密码。。在#solving a quadratic equation
#finding conjugate pair of imaginary roots if no real roots
import math
import sys
a = float(input('Enter a: '))
b = float(input('Enter b: '))
c = float(input('Enter c: '))
try:
# find the discriminant
d = (b**2)-(4*a*c)
if d < 0:
import cmath #importing complex math to find imaginay roots
x1 = (-b-cmath.sqrt(d))/(2*a)
x2 = (-b+cmath.sqrt(d))/(2*a)
print"This equation gives a conjugate pair of imaginary roots: ", x1, " and", x2
elif d == 0:
x = (-b+math.sqrt(d))/(2*a)
print "This equation has one solutions: ", x
else:
x1 = (-b+math.sqrt(d))/(2*a)
x2 = (-b-math.sqrt(d))/(2*a)
print "This equation has two solutions: ", x1, " and", x2
except:
SystemExit
我需要使我的程序健壮,以防用户输入非数字数据。我尝试过使用条件语句(if..elif..else)和异常处理。在
我需要程序显示一个错误消息,并提示用户再试一次
帮助