-4
a = int(raw_input("A?"))
b = int(raw_input("B?"))
c = int(raw_input("C?"))

minusb = -b
bsquared = b**2
fourac = 4*a*c
twoa = 2*a
discriminant = bsquared + fourac

if discriminantt<0:
  print "This quadratic have no real root."
elif determinant>=0:
  answer1 = (minusb+((bsquared-fourac)**0.5))/twoa
  answer2 = (minusb-((bsquared-fourac)**0.5))/twoa
  print "X = %s and %s" % (answer1, answer2)

However, when the determinant is less than 0, instead of printing it simply runs an error message saying that answer1 and answer2 can't be done. How do you make the program stop running if discriminant<0?

PS: This is me simply trying to practice what I learnt online. Sorry if my code is terrible XD

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
PCK11800
  • 1
  • 1
  • Do you mean the discrimant instead of the determinant? Because that would be bsquared - fourac, and that's the value you want to check for being >= 0. –  Sep 23 '17 at 10:42
  • The value you're currently checking for being >= 0 is not the value that you take the square root of, hence even after the check, your program may still attempt to take the square root of a negative value, causing the error. –  Sep 23 '17 at 10:44
  • you are right XD, changed it – PCK11800 Sep 23 '17 at 10:47
  • so do you want the program to *print* (as per title) or to **stop** (as per question). – umläute Sep 23 '17 at 10:51
  • Show the error message. –  Sep 23 '17 at 10:51
  • then you should fix the question. also there's a simple logic error: you are testing for `bsquared + fourac` but the problem is that you cannot get the square-root of `bsquared - fourac` (as @Evert has pointed out) – umläute Sep 23 '17 at 10:56
  • You changed it to `bsquared + fourac`; that's not going to help either. It's `bsquared` minus `fourac`. –  Sep 23 '17 at 10:58
  • sorry i am so stupid :P – PCK11800 Sep 23 '17 at 11:00

4 Answers4

0
a = int(raw_input("A?"))
b = int(raw_input("B?"))
c = int(raw_input("C?"))

minusb = -b
bsquared = b**2
fourac = 4*a*c
twoa = 2*a
determinant = bsquared-fourac

if determinant<0:
    print "This quadratic have no real root."
elif determinant>=0:
    answer1 = (minusb+((bsquared-fourac)**0.5))/twoa
    answer2 = (minusb-((bsquared-fourac)**0.5))/twoa
    print "X = %s and %s" % (answer1, answer2)

For a quadratic discrimant=bsqaured-4*a*c depending on its value roots are real or imaginary

Code_ninja
  • 117
  • 1
  • 10
-1

The Python way to catch is errors is to catch exceptions using the try/except construct, rather than trying to prevent the error to happen. E.g.:

try:
   answer1 = (minusb+((bsquared-fourac)**0.5))/twoa
   answer2 = (minusb-((bsquared-fourac)**0.5))/twoa
   print("X = %s and %s" % (answer1, answer2))
except ValueError as e:
   print("oops: %s" % e)

The exception will inherit from the Exception class, but in practically all cases you will want to catch the specific exception, e.g. (in the above example) an exception of type ValueError. You can easily get the type of exception you need to catch by

  • reading the documentation
  • just running the code (without an except clause) and reading the error message: it will give the type of the Exception being raised.

You can do whatever error-handling you deem appropriate in the exception handler. e.g. the following will exit the program if it catches the error:

import sys
try:
   answer1 = (minusb+((bsquared-fourac)**0.5))/twoa
   answer2 = (minusb-((bsquared-fourac)**0.5))/twoa
   print("X = %s and %s" % (answer1, answer2))
except ValueError as e:
   print("oops: %s" % e)
   sys.exit(1)

also, print "foo" is no longer valid in Python3 (which you should use by all means); use print("foo") instead (which is valid in Python2 as well)

umläute
  • 28,885
  • 9
  • 68
  • 122
  • why the downvote? the title says "print instead of error" (the question says "stop instead of error") – umläute Sep 23 '17 at 10:47
-1

Just use

import sys
sys.exit(1)

Exit terminates the run. The 1 can be any number you prefer. usually 0 is used to say that the program was executed successfully, and 1 means that an error occured. Read more about this here How to exit from python

ronenmiller
  • 1,117
  • 15
  • 25
-1

Look at your code, You are doing a mistake due to which your code is not working.

Look at these three lines of codes:

discriminant = bsquared + fourac

if discriminantt<0:

elif determinant>=0:

You are using three different variables unknowingly.

10 Rep
  • 2,217
  • 7
  • 19
  • 33
Shiva Gupta
  • 402
  • 1
  • 3
  • 13