-2

I don't get any error messages, but my code doesn't print the x values.

    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))
bence
  • 49
  • 1
  • 6
  • 2
    Please fix the indentation in your code. – Aran-Fey Sep 20 '17 at 10:29
  • You input a, b, and c, and you define a function called `roots`, and that's it. If you want the code in `roots` to be executed, you have to *call* the function. The only call to `roots` is from within `roots` itself, after the returns. Move it outside of the function. – Tom Karzes Sep 20 '17 at 10:29
  • you're not calling `roots`. Remove that tab before `print`. – isalgueiro Sep 20 '17 at 11:07

2 Answers2

0

You have indented print(roots(a,b,c)). This line should be at zero indentation since this is not part of the function definition -you are calling the function.

Varun Balupuri
  • 363
  • 5
  • 17
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))

Properly indent your code and you will get your answer.

P.Madhukar
  • 454
  • 3
  • 12
  • It worked, thanks. But since i have complex numbers in some solutions how do i perform a unittest? – bence Sep 20 '17 at 10:40
  • if you want to perform more complex arithmetic calculation with real, imaginary or complex numbers then you should use http://deeplearning.net/software/theano/introduction.html package – P.Madhukar Sep 20 '17 at 10:43