In: Computer Science
2,3,1
8,7,-3
1,2
Abc
0,4,3
-1,7,10
Develop a Python program which reads all the coefficient inputs from coeff.txt and find the “real” roots of the equations. The following requirements must be met:
1). ANNSWER :
GIVENTHAT :
Check the output.txt file which display the equation with their roots and valid or invalid euqations.
Please check below screenshot for code indentation and output.
Code:
file=open("coeff.txt",'r')
list1=file.readlines()
list2=[]
for i in list1:
list2.append(i.split(","))
file.close()
f = open("output.txt", "w+")
for i in list2:
if(len(i)==3):
a=int(i[0])
b=int(i[1])
c=i[2][0:len(i)-1]
c=int(c)
a=int(a)
b=int(b)
f.write(i[0]+"x^2+"+i[1]+"x+"+str(c)+" = ")
d = (b ^ 2) - (4 * a * (c))
if (d < 0):
f.write("No real roots or Roots are imaginary \n")
else:
root1 = (-b + d) / 2 * a
root2 = (-b - d) / 2 * a
f.write("Roots are "+ str(round(root1, 2))+"\t"+str(round(root2, 2))+"\n")
else:
f.write("Equations are not valid \n")
f.close()
Output:

