-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquadraticformula.py
54 lines (48 loc) · 1.85 KB
/
quadraticformula.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import math
print("")
print(" Welcome to Quadratic")
print(" Formula Solver!")
print(" Created by Spencer Boggs")
print("")
print(" Make sure to use the correct")
print(" variables.")
print(" Equation: ax^2 + bx + c")
print("")
print("")
input(" Press [Enter] to Start")
while True:
negativeInSqrt = False
print('')
a = input("Enter a: ")
if (float(a) != 0):
b = input("Enter b: ")
c = input("Enter c: ")
print('')
if (a) and (b) and (c):
a = float(a)
b = float(b)
c = float(c)
print("Equation: (-" + str(b) + " +- sqrt(" + str(b) + "^2 - 4(" + str(a) + ")(" + str(c) + ")))/2(" + str(a) + ")")
try: math.sqrt((b**2) - 4 * a * c)
except ValueError: negativeInSqrt = True
if (negativeInSqrt != True):
solvedSqrt = math.sqrt((b**2) - 4 * a * c)
else:
negativeValue = (abs((b**2) - 4 * a * c))
solvedSqrt = "i*sqrt(" + str(negativeValue) + ")"
if (negativeInSqrt != True):
posAnswer = -b + solvedSqrt
print("Plus Sqrt: " + str(posAnswer/(2 * a)))
negAnswer = -b - solvedSqrt
print("Minus Sqrt: " + str(negAnswer/(2 * a)))
else:
positiveNumerator = "-" + str(b) + " + " + str(solvedSqrt)
print("Plus Sqrt: (" + str(positiveNumerator) + ")/" + str(2 * a))
negativeNumerator = "-" + str(b) + " - " + str(solvedSqrt)
print("Minus Sqrt: (" + str(negativeNumerator) + ")/" + str(2 * a))
else:
print("Please enter valid")
print("variables")
else:
print("a cannot be 0. Please")
print("try again")