mardi 8 septembre 2015

Faulty if less than statement python

When I call the following function:

def check_r(x, y, y_check):
print 'CHECK_R'
original = r(x,y)
print 'Original:'+str(original)
if original<1.0:
    print 'Original less than 1.0'
    check = r(x,y_check)
    print check
    if check>original:
        return y_check
    else:
        return y
else:
    return y

Where r is a function that in this case returns 1.0. It outputs:

CHECK_R

Original:1.0

Original less than 1.0

I think I'm missing something obvious, but I've checked the two variables are both floats so there shouldn't be any pythonic variable interpretation issues.

I've run similar code that outputs the correct answer so it's clearly an error in my code but I can't identify it.

I can't find anything online about people having similar issues (and I couldn't find anything relevant when I looked on here), and the people I work with can't identify the problem either.

Can someone explain why this is happening? I'm assuming it's me messing up the syntax, but if it's a subtler python feature it would be really useful to know about. Thanks!

Aucun commentaire:

Enregistrer un commentaire