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

hey, I tried to run this prog. x=int(raw_input('enter an integer: ')) ans=0 while ans*ans==x: ans =ans + 1 print 'the square root of ' + str(x) + 'is' + str(ans) ---------------------------------------- can anyone point out the problem. the output says the square root of 25 is 0. of course ill be only entering perfect square roots.

OpenStudy (anonymous):

You implemented the increment, but did not put in the decision element necessary to determine if the input has a square root. That decision structure is covered in lecture 3, but here is a crude example to show you what I'm talking about: x=int(raw_input('enter an integer: ')) ans = 0 while ans*ans < x: ans = ans + 1 if ans*ans == x: print 'the square root of ' + str(x) + ' is ' + str(ans) else: print 'the square root of ' + str(x) + 'is not an integer' Note that I did not put in the requisite code to determine a non-integer root (lecture covers that nicely).

OpenStudy (anonymous):

@jmike.. i see what i missed.. thank you...

OpenStudy (anonymous):

No problem, glad I could help.

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!