How to change value inside function
<p>Very new to coding. I'm trying to learn myself Python by doing a little project I came up with. I realize this will be a slow process, but I just had a question about loops.</p>

<p>This is what I'm trying to do:</p>

<p>-The user inputs a list of numbers, and if a number in the list is more than 360, the function will subtract 360 from it until it is below 360.</p>

<p>-Once it's below 360:</p>

<ul>
<li>If it's above 270, it will subtract the number from 360.</li>
<li>If it's above 180, it will subtract 180 from the number.</li>
<li>If it's above 90, it will subtract the number from 180.</li>
</ul>

<p>-It should then print the values. If this process sounds familiar, I'm trying to convert an azimuth to a bearing. Right now I'm just focused on getting the numerical value, then I'll add the direction.</p>

<p>This is my code:</p>

<pre><code>    def bearing(x):
        for i in range(len(x)):
            while x[i]&gt;=360:
                x[i]-=360
            if x[i]&gt;270:
                x[i]==360-x[i]
            elif x[i]&gt;180:
                x[i]-=180
            elif x[i]&gt;90:
                x[i]==180-x[i]
        print (x)
</code></pre>

<p>The while loop works fine, but it stops there. I'm not sure if my indenting is wrong, or I'm using the wrong commands, but any help would be greatly appreciated.</p>

<p>You may or may not be familiar with the modulus operator (<code>%</code>). What that does is return the remainder from division, which is basically caffeinated subtraction anyway. For example, <code>5 % 3</code> would return <code>2</code> since <code>5 / 3 == 1 remainder 2</code>. Hopefully you can see how <code>user_input % 360</code> would take care of your initial subtraction to <code>&lt; 360</code>.</p>

<p>Translating this to a list of numbers: <code>list_below_360 = [x % 360 for x in user_input_list]</code>.</p>

<p>So now, let's assume we have properly formatted user input that is a list of integers all below 360.</p>

<pre><code>for i in range(len(list_below_360)):
    if list_below_360[i] &gt; 270:
        list_below_360[i] = 360 - list_below_360[i]
        #notice the use of a single equals sign here,
        #a double equals sign does comparison, not assignment
    elif list_below_360[i] &gt; 180:
        list_below_360[i] -= 180
    elif list_below_360[i] &gt; 90:
        list_below_360[i] = 90 - list_below_360[i]
print(list_below_360)
</code></pre>

<p>Hope this helps.</p>

python loops