0

my code is giving me some errors Traceback (most recent call last): File "python", line 7, in ValueError: math domain error

  import math
  a= 3
  b= 5
  c= 2
  d= b^2 -4*a*c
  x1 = math.sqrt(d)
  print(x1)
Manil Puri Manil
  • 117
  • 1
  • 1
  • 6
  • 3
    I think `b^2` should probably be `b**2` – Aran-Fey Nov 18 '17 at 13:39
  • 2
    `^` is bitwise xor, you want to use `**` for exponentiation. – Abhijith Asokan Nov 18 '17 at 13:40
  • 1
    Possible duplicate of [ValueError: math domain error](https://stackoverflow.com/questions/15890503/valueerror-math-domain-error) – Aran-Fey Nov 18 '17 at 13:40
  • So you're aware - "why isn't this code working?" is a defined "close" vote option on stack overflow in itself (people can vote to close a question so that it can't be answered unless the question is edited to meet the site guidelines). Your title is not helpful for others that might encounter the same problem. – roganjosh Nov 18 '17 at 13:45

2 Answers2

0

Your d is -17 (You most probably wanted to use ** instead of ^)

What is the root of a negative number?

That is what the exception is saying

Nabin
  • 11,216
  • 8
  • 63
  • 98
0

d is negative when there are no real solutions, therefore its square roor is also not real:
Please also note that b^2 is not bsquared, it is b xor 2. for b square, use b**2, or b*b

import math

a = 3
b = 5
c = 2

d = b**2 - 4*a*c      # Attention, b^2 is not b square, use b**2
if d > 0:
    x1 = math.sqrt(d)
    print(x1)
else:
print("there are no real roots")
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80