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.
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).
@jmike.. i see what i missed.. thank you...
No problem, glad I could help.
Join our real-time social learning platform and learn together with your friends!