Python round with `n // 1`
Question: I was wondering if there is any reason not to use the `//` operator to round a
number to an integer. I didn't see much on this topic or really know what to
look for to find out more.

    
    
    >>> from random import random
    >>> random() * 20 // 1
    1.0
    >>> random() * 20 // 1
    0.0
    >>> random() * 20 // 1
    16.0
    >>> random() * 20 // 1
    11.0
    >>> random() * 20 // 1
    0.0
    

Besides needing to add 1 to the result (to not get a range of 1-20 instead of
0-19) Or does this actual result in the range 0-20?


Answer: The main reason not to use it would be that there are perfectly good in-built
functions `int` and `round` that already do this, which are likely to be
efficient and will not confuse someone reading your code.

Another reason as pointed out in the comments is that `//1` is equivalent to
`math.floor` rather than `int` or `round`. `-2.5 // 1` returns a float `-3.0`
whereas `int(-2.5)` returns an integer `-2`.

The fact that experienced programmers can be confused about what `//1` does,
illustrates nicely why it is better to use one of the existing functions
designed for this purpose -- they behave in ways that are clearly defined,
documented and consistent.

