Float behavior


#1

To make sure I understood how the Float () function worked, I did this.

cucumbers = 100
num_people = 6

whole_cucumbers_per_person = cucumbers/num_people
print whole_cucumbers_per_person

float_cucumbers_per_person = 100./6
print float_cucumbers_per_person

float (cucumbers)
whole_cucumbers_per_person = cucumbers/num_people
print whole_cucumbers_per_person

Going by what was explained on this page, I expected to get 3 printed lines, the first of which would be an integer quotient, and the second two of which would be float quotients.

However, what I got instead was this:

16
16.6666666667
16

the third printed line, which should represent the quotient of a float and an integer, is showing up as an integer quotient instead of a float quotient.

Why is it doing that?


Cucumber logic
#2

@tengutastic,

Line 10 by itself gives the modified variable to work with (for example, in the middle of an expression), but doesn’t change the variable’s value for next time you call it. You need to save the variable’s new value to itself, like this:

cucumbers = float(cucumbers)