Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 6 Online
OpenStudy (anonymous):

I started yesterday the course and today tried to make a small program, but doesn't seem to work as I want print "(a*(x**2))+(b*x)+c" a=+3 b=-5 c=+2 D=(b**2-4*a*c) x1=(-b+(D**(-1/2)))/(2*a) x2=(-b-(D**(-1/2)))/(2*a) if D>0 and (-b+(D**(-1/2)))/(2*a)>0 and (-b-(D**(-1/2)))/(2*a)>0: print "x1=" x1 print "x2=" x2 elif D>0 and (-b+(D**(-1/2)))/(2*a)>0: print "x1=" x1 elif D>0 and (-b-(D**(-1/2)))/(2*a)>0: print "x2"= x2 else: print "x not found" ok the part of printing the quadratic equation with a,b and c changed with numbers is the difficult.

OpenStudy (anonymous):

but I also want to show the solution as x1= number. Can someone please explain what is wrong? I have only seen the first two video lectures, so I am not an expert... Thank you very much for your time!!!

OpenStudy (anonymous):

What programming language are you using? Python?

OpenStudy (anonymous):

oh. yes Python. sorry i didn't mention it.

OpenStudy (anonymous):

It's ok :) What version of python, 2 or 3?

OpenStudy (anonymous):

2

OpenStudy (anonymous):

Actually, you will have to do this: a=+3 b=-5 c=+2 D=(b**2-4*a*c) x1=(-b+(D**(-1/2)))/(2*a) x2=(-b-(D**(-1/2)))/(2*a) print "(a*(x**2))+(b*x)+c" if D>0 and (-b+(D**(-1/2)))/(2*a)>0 and (-b-(D**(-1/2)))/(2*a)>0: print "x1=" x1 print "x2=" x2 elif D>0 and (-b+(D**(-1/2)))/(2*a)>0: print "x1=" x1 elif D>0 and (-b-(D**(-1/2)))/(2*a)>0: print "x2"= x2 else: print "x not found"

OpenStudy (anonymous):

I think you have to put the definitions BEFORE you tell it to print things. Also, make sure you indent the else like this: else: print "Indent the else!"

OpenStudy (anonymous):

it says, invalid syntax at: "x1=" x1 =( I tried to run it exactly as you said

OpenStudy (anonymous):

Instead of print"x1=" x1, do this: print"x1=" + x1 print"x2=" + x2

OpenStudy (anonymous):

a=+3 b=-5 c=+2 D=(b**2-4*a*c) x1=(-b+(D**(-1/2)))/(2*a) x2=(-b-(D**(-1/2)))/(2*a) print "(a*(x**2))+(b*x)+c" if D>0 and (-b+(D**(-1/2)))/(2*a)>0 and (-b-(D**(-1/2)))/(2*a)>0: print "x1=" + x1 print "x2=" + x2 elif D>0 and (-b+(D**(-1/2)))/(2*a)>0: print "x1=" + x1 elif D>0 and (-b-(D**(-1/2)))/(2*a)>0: print "x2=" + x2 else: print "x not found"

OpenStudy (anonymous):

>>> (a*(x**2))+(b*x)+c Traceback (most recent call last): File "C:/Users/Klaou/Desktop/hi.py", line 52, in <module> print "x1=" + x1 TypeError: cannot concatenate 'str' and 'float' objects >>>

OpenStudy (anonymous):

Then do this: a=+3 b=-5 c=+2 D=(b**2-4*a*c) x1=str(-b+(D**(-1/2)))/(2*a) x2=str(-b-(D**(-1/2)))/(2*a) print "(a*(x**2))+(b*x)+c" if D>0 and (-b+(D**(-1/2)))/(2*a)>0 and (-b-(D**(-1/2)))/(2*a)>0: print "x1=" + x1 print "x2=" + x2 elif D>0 and (-b+(D**(-1/2)))/(2*a)>0: print "x1=" + x1 elif D>0 and (-b-(D**(-1/2)))/(2*a)>0: print "x2=" + x2 else: print "x not found"

OpenStudy (anonymous):

yes yes yes, basically you did the number string! so this is solved, but the result remains (a*(x**2))+(b*x)+c x1=1.0 x2=0.666666666667 I changed the code to something like that:

OpenStudy (anonymous):

a,b,c = input("Enter the coefficients of a, b and c separated by commas: ") print "(a*(x**2))+(b*x)+c" D=(b**2-4*a*c) x1=str((-b+(D**(-1/2)))/(2*a)) x2=str((-b-(D**(-1/2)))/(2*a)) if D>0 and (-b+(D**(-1/2)))/(2*a)>0 and (-b-(D**(-1/2)))/(2*a)>0: print "x1=" + x1 print "x2=" + x2 elif D>0 and (-b+(D**(-1/2)))/(2*a)>0: print "x1=" + x1 elif D>0 and (-b-(D**(-1/2)))/(2*a)>0: print "x2=" + x2 else: print "x not found"

OpenStudy (anonymous):

Enter the coefficients of a, b and c separated by commas: 3,-5,2 (a*(x**2))+(b*x)+c x1=1.0 x2=0.666666666667

OpenStudy (anonymous):

so the a,b,c is still there :( want it to be 3,-5,2 But really thanks for the help!!!!

OpenStudy (anonymous):

You're welcome!

OpenStudy (anonymous):

Finally I've made it! : a,b,c = input("Enter the coefficients of a, b and c separated by commas: ") from math import sqrt x="x" print ((str(a) + x) + "^2")+( "[+]" + str(b) + x)+( "[+]" + str(c)) D=((b**2)-(4*a*c)) print "D=" + str(D) if D>0: sqrt_rootD=(sqrt(D)) print "sqrt_rootD=" + str(sqrt_rootD) x1=((-b+(sqrt(D)))/(2*a)) print "x1=" + str(x1) x2=((-b-(sqrt(D)))/(2*a)) print "x2=" + str(x2) elif D==0: x3=(-b/(2*a)) print "x3=" + str(x3) else: print "x not real number"

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!