#Find the cube root of a perfect cube x = int(raw_input('Enter an integer: ')) ans = 0 while ans*ans*ans < abs(x): ans = ans + 1 #print 'current guess =', ans if ans*ans*ans != abs(x): print x, 'is not a perfect cube' else: if x < 0: ans = -ans print 'Cube root of ' + str(x) + ' is ' + str(ans) Okay look at the while loop. You see the Why is it a less than? Don't we want an =? I don't understand why we can't put an =. If our integer was 27, 3*3*3 = 27, not < 27.
Let's look at an example to help you understand: let's say we enter 27 for the integer while loop: 0*0*0 < 27? yes ans = 1 1*1*1 < 27? yes ans = 2 2*2*2 < 27? yes and = 3 3*3*3 < 27? no while loop finished 3*3*3 != 27? no, cube root found __________________________ if we had used '==' in that while loop it would have done this: 0*0*0 == 27? no while loop finished 0*0*0 != 27? yes 27 is not a perfect cube
You're great. Thanks so very much!
You're welcome. If this resolved the question would you please close it?
Join our real-time social learning platform and learn together with your friends!