I'm trying to create a code that will solve for x using the quadratic formula. For the outputs I don't want it to display the imaginary numbers...only real numbers. I set the value inside the square root to equal the variable called "root" to determine if this value would be pos/neg (if it's neg, then the solution would be imaginary).
This is the code.
import math
print("Solve for x when ax^2 + bx + c = 0")
a = float(input("Enter a numerical value for a: "))
b = float(input("Enter a numerical value for b: "))
c = float(input("Enter a numerical value for c: "))
root = math.pow(b,2) - 4*a*c
root2 = ((-1*b) - math.sqrt(root)) / (2*a)
root1 = ((-1*b) + math.sqrt(root)) / (2*a)
for y in root1:
if root>=0:
print("x =", y)
elif root<0:
print('x is an imaginary number')
for z in root2:
if root>=0:
print("or x =", z)
elif root<0:
print('x is an imaginary number')
This is the error code:
File "/Users/e/Documents/Intro Python 2020/Project 1/Project 1 - P2.py", line 25, in <module>
for y in root1:
TypeError: 'float' object is not iterable
The error occurs at the line:
for y in root1:
How do I fix this error?
for y in root1exactly?