[Tutor] Having Unusual results

Dave Angel d at davea.name
Sat May 2 12:54:57 CEST 2015


On 05/02/2015 04:36 AM, Peter Otten wrote:
> Jag Sherrington wrote:
> With that the calculation becomes
>
>>>> buns = 20
>>>> package_size = 8
>>>> whole_packages, missing_buns = divmod(buns, package_size)
>>>> total_packages = whole_packages
>>>> if missing_buns: total_packages += 1
> ...
>>>> total_packages
> 3
>

And that can be simplified:

buns = 20
package_size = 8
total_packages = (buns + package_size - 1) // package_size

#desired answer 3


Or, to take better advantage of the Python library:

import math
total_packages = math.ceil(buns/package_size)

This is exactly what the ceiling and floor mathematical concepts are 
needed for.

Note, I'm using the fact that the OP is writing in Python 3.  If not, 
one should probably add
    from __future__ import division
.

-- 
DaveA


More information about the Tutor mailing list