Why the heck there is brackets un the cucumbers and not on the entire division??? Doesn’t make any sense
float_cucumbers_per_person = float(cucumbers)/num_people
Please look at the following example:
print float(50 / 3) # 16.0
print float(50) / 3 # 16.6666666667
the problem is that python2 rounds down to the nearest integer if the division involves two integers.
When we want a float as result, casting to float after the division won’t help, given rounding down has already happened.
Which is why we need to convert one of two integers to float first, given a float divided by integer (or vice versa) gives a float as result
4 Likes
we can do that:
(50 + 0.) / 3 # 16.6666666667
Please explain sir elaborately
doing 50 + 0. will give 50.0 which is a float, so the division also goes as planned