二次公式根(a,b,c)函数。蟒蛇

时间:2017-09-20 10:24:49

标签: python formula quadratic

我没有收到任何错误消息,但我的代码没有打印x值。

    from math import sqrt
a= float(input("a= "))
b= float(input("b= "))
c= float(input("c= "))
def roots(a,b,c):
    disc = b**2 - 4*a*c
    if disc >= 0:
        return ("x= ",(-b + sqrt(disc))/(2*a), "x= ",(-b - sqrt(disc))/(2*a))
    if disc < 0:
        return ("x= ",-b/(2*a),"+",sqrt(disc*(-1))/(2*a),"i" \
                "x= ",-b/(2*a),"-",sqrt(disc*(-1))/(2*a),"i")
    print(roots(a,b,c))

2 个答案:

答案 0 :(得分:0)

您缩进了print(roots(a,b,c))。这一行应该是零缩进,因为这不是函数定义的一部分 - 你正在调用函数。

答案 1 :(得分:0)

from math import sqrt
a= float(input("a= "))
b= float(input("b= "))
c= float(input("c= "))
def roots(a,b,c):
    disc = b**2 - 4*a*c
    if disc >= 0:
        return ("x= ",(-b + sqrt(disc))/(2*a), "x= ",(-b - sqrt(disc))/(2*a))
    if disc < 0:
        return ("x= ",-b/(2*a),"+",sqrt(disc*(-1))/(2*a),"i" \
                "x= ",-b/(2*a),"-",sqrt(disc*(-1))/(2*a),"i")
print(roots(a,b,c))

正确缩进代码,您将得到答案。