Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. import sys
  2. import math
  3.  
  4.  
  5. def quadratic(a, b, c):
  6.     disc = b**2 - 4*a*c
  7.     if disc < 0:
  8.         raise ValueError("There are no real solutions.")
  9.     if disc == 0:
  10.         return -b / (2*a)
  11.     else:
  12.         return (-b + math.sqrt(disc)) / (2*a), (-b - math.sqrt(disc)) / (2*a)
  13.  
  14.  
  15. if len(sys.argv) < 4:
  16.     print("A quadratic needs three coefficients")
  17.     exit()
  18.  
  19. try:
  20.     a = int(sys.argv[1])
  21.     b = int(sys.argv[2])
  22.     c = int(sys.argv[3])
  23.     if a == 0:
  24.         raise ValueError("Not a valid quadratic: the leading coefficient cannot be zero.")
  25.  
  26.     print(f"The equation {a}x^2 + {b}x + {c} = 0 has these solutions:")
  27.     if isinstance(quadratic(a,b,c), float):
  28.         x_1 = quadratic(a, b, c)
  29.         print(f"x = {x_1:.2f}")
  30.     else:
  31.         x_1, x_2 = quadratic(a, b, c)
  32.         print(f"x = {x_1:.2f} and x = {x_2:.2f}")    
  33. except ValueError as e:
  34.     print(e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement