Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Python 3.6- Okay, I got a problem that can potentially be solved quickly. The first img is a txt file called quad.txt. The second is
Python 3.6- Okay, I got a problem that can potentially be solved quickly. The first img is a txt file called quad.txt. The second is source code that writes( to a diff txt file called quadresults.txt) the x intercepts by solving qudratic equation.
For line 9, I should get f(x) = 1x^2 +2x+1 which only has x intercept at (-1,0,0). However , I get Wrong amt of coefficients! as the output which means that my script needs correction. Correct the source code ,such as the quadratic forumula, so it displays for line 9 that f(x)= 1x^2 +2x + 1. Thanks
def quadratic(): with open('quad.txt', 'r') as f: # read-only file with open('quadresults.txt', 'w') as w: # opens and writes on file for line in f: try: a, b, c = (int(x) for x in line.split()) d = int(b) ** 2 - 4 * int(a) * int(c) # discriminant if d elif d == 0: x = (-int(b) + (int(b) ** 2 - 4 * int(a) * int(c)) ** .5) / 2 * int(a) # one solution w.write('f(x) =' + str(a), 'x^2+'+ str(b)+ 'x+'+ str(c)+ " which only has x intercept at"+ str(x) + ' ') else: x1 = (-int(b) + (int(b) ** 2 - 4 * int(a) * int(c)) ** .5) / 2 * int(a) x2 = (-int(b) - (int(b) ** 2 - 4 * int(a) * int(c)) ** .5) / 2 * int(a) # for two solutions w.write('f(x) ='+ str(a)+ 'x^2+'+ str(b)+ 'x+'+ str(c)+ "with x-intercepts at "+ \ format(x1, ',.2f')+ " and"+ format(x2, ',.2f') + ' ') # print to two decimal places except Exception as e: if len(line) != 3: w.write('Wrong amount of coefficients! ') else: if str() in line: w.write('Invalid coefficients or constant! ') quadratic()
5 7 1 6 10 9 8 20 10 100 600 600 63 2 62 (3 9 1 2 1 -9-5-5
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started