Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Question on while condition

EnverEnver IndiaMember Posts: 1

to find a cube root of a number

x = int(raw_input ('Enter an integer'))
ans = 0
while ansansans < abs(x):
ans = ans + 1

print 'current guess =', ans

Say the integer i input is 55. so why do I get a 'current guess' value up to 4. Shouldn't it stop at 3, since the while condition says: ans * ans * ans until the product is less then the absolute value of the number entered/assumed (in this case 55)

Comments

  • B00B00 Member Posts: 94 ✭✭✭

    It does exactly what it's supposed to:
    ans = 3.
    Is 27 less than 55?
    Yes.
    ans = 4.
    Is 64 less than 55?
    No.
    Break the cycle.
    Print 4.

  • salenaadamsalenaadam USAMember Posts: 3

    Calculation of a cube root by hand is similar to long-hand division or manual square root. Suppose you need to find the cube root of 55,742,968. Set up a "division" with the number under the radical. Mark off triples of digits, starting from the decimal point and working left.

Sign In or Register to comment.