samedi 20 juin 2015

A confusion in Python's If else logic

I am learning python this summer online from MITx: 6.00.1x. Here's the code

    x = 23.0
    epsilon = 0.01
    step = 0.1
    guess = 0.0

    while abs(guess**2-x) >= epsilon:
        if guess <= x:
            guess += step
        else:
            break

    if abs(guess**2 - x) >= epsilon:
        print "outside"
        print guess
    else:
        print 'succeeded: ' + str(guess)

According to me, the while loop should break when the value of guess becomes 23.1 . because when guess=23,the if condition, guess<=23, will satisfy and the next line guess+=step should execute which will make guess=23.1 and then in the next iteration, guess<=23.0 will not be satisfied and the loop will break. But when I run the code the output comes out to be

outside

23.0

shouldn't it be 23.1 ?

Aucun commentaire:

Enregistrer un commentaire