Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import math
- def quadratic(a, b, c):
- disc = b**2 - 4*a*c
- if disc < 0:
- raise ValueError("There are no real solutions.")
- if disc == 0:
- return -b / (2*a)
- else:
- return (-b + math.sqrt(disc)) / (2*a), (-b - math.sqrt(disc)) / (2*a)
- if len(sys.argv) < 4:
- print("A quadratic needs three coefficients")
- exit()
- try:
- a = int(sys.argv[1])
- b = int(sys.argv[2])
- c = int(sys.argv[3])
- if a == 0:
- raise ValueError("Not a valid quadratic: the leading coefficient cannot be zero.")
- print(f"The equation {a}x^2 + {b}x + {c} = 0 has these solutions:")
- if isinstance(quadratic(a,b,c), float):
- x_1 = quadratic(a, b, c)
- print(f"x = {x_1:.2f}")
- else:
- x_1, x_2 = quadratic(a, b, c)
- print(f"x = {x_1:.2f} and x = {x_2:.2f}")
- except ValueError as e:
- print(e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement