Python make a number loop within a range
<p>So I saw this post, <a href="http://stackoverflow.com/q/5996881/6759115">on limiting numbers,</a> but I noticed it would cut a number off if it wasn't within the range.</p>

<p>Is there a way to do this like so:</p>

<p><code>variable = 25</code></p>

<p>If your range is 1-20, can <code>variable</code> become 5?</p>

<p>Or if <code>variable = 41</code>, have it become 1?</p>

<p>I have tried this:</p>

<pre><code> def loop(number, maxn, minn):
     if number &gt; maxn:
         number = maxn - number
     return number
</code></pre>

<p>With a range of 0-20, and a number of 40, I got -20?!?</p>

<p>Just figured it out.</p>

<pre><code>def loop(n, minn, maxn):

    while n &gt; maxn:
        n = n - (maxn - minn)
    while n &lt; minn:
        n = n + (maxn - minn)

    return n
</code></pre>

<p>I think that should work for most cases, including negative numbers and ranges.</p>

python