import math print("Bryce's Quadratic Equation Solver.") print("This program uses the quadratic formula to find x, given the a, b, and c values (a(x^2) + bx + c = 0)") def solve(): global a global b global c global radical global isImaginary isImaginary = False a = int(input("In your equation, what is a? ")) if (a == 0): print("Your equation is linear. Please enter a new equation.") solve() b = int(input("What is b? ")) c = int(input("What is c? ")) radical = ((b ** 2) - (4 * a * c)) if radical < 0: print("Just for your information, x will be imaginary. Do you want to enter a (n)ew equation or (c)ontinue?") if input().lower() == "n": solve() else: isImaginary = True radical = radical * -1 radical = math.sqrt(int(radical)) radical = "(i * " + str(radical) + ")" solvePlus() else: solvePlus() def solvePlus(): global xPlus xPlus = (b * -1) if isImaginary: return str(xPlus) + " + " + str((radical)) else: xPlus = xPlus + math.sqrt(int(radical)) xPlus = xPlus / (2 * a) return xPlus solveMinus def solveMinus(): global xMinus xMinus = (b * -1) if isImaginary: return str(xPlus) + " - " + str((radical)) else: xMinus = xMinus - math.sqrt(int(radical)) xMinus = xMinus / (2 * a) return xMinus solve() #Debug code print("radical == " + str(radical)) #delete after use print("x is " + str(solvePlus()) +" and/or " + str(solveMinus()))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question