I'm trying to divide number into groups in plain python without importing or the use of if-statements, and get the division into groups + remainder forming one extra group so that 200 / 99 would be 3, And 7 / 3 would be 3, but that 8 / 4 would still be just 2, and 4 / 2 would be 2 etc.. I cannot import anything so it needs to be in plain python.
I tried storing the numbers from inputs from user into variables and dividing them, and then adding one. I also tried // and adding 1 but I cannot get it to work.
How about this:
This computes the result by performing an integer divide
a // band computing the remaindera % b. Converting an integer value to a boolean isFalsefor 0 andTruefor any other value, and converting that back to an integer gives you the value you want to add to the result. The remainder is computed again to assign it, if you need it.As user @markransom commented, the conversion to
int()isn't even necessary, asboolalready 'is' an integer type:So, this works (although it may be considered a bit less readable):
If you're using a modern version of Python and really want it to be short, this also works:
This uses the walrus operator to assign the
remainderwhen it is first computed, avoiding having to compute it twice as well.