数学域错误python代码未运行

时间:2019-07-18 19:12:12

标签: python

 it is giving math domain error
  1. 列表项
import math

b = float(input("enter b: "))
a = float(input("enter a: "))
c = float(input("enter c: "))
y = ((b*b) - 4*a*c)
# the solution is giving errors here
m = math.sqrt(y)
  1. 方程式
z = -b - m
x = -b + m
h = z/2*a
i = x/2*a

1 个答案:

答案 0 :(得分:0)

来自math docs

  

当前实现将针对无效情况提高ValueError   sqrt(-1.0)log(0.0)之类的操作(其中C99附件F建议使用   表示无效操作或被零除)


您可以在致电y >= 0之前检查是否sqrt

或用try/except换行:

if y >= 0:  # try:
    m = math.sqrt(y)
else:       # except ValueError:
    print("Delta smaller than 0, no real solution")