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.
This is what I'm trying to do:
-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.
-Once it's below 360:
-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.
This is my code:
def bearing(x):
for i in range(len(x)):
while x[i]>=360:
x[i]-=360
if x[i]>270:
x[i]==360-x[i]
elif x[i]>180:
x[i]-=180
elif x[i]>90:
x[i]==180-x[i]
print (x)
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.
Instead of using assignment (=
) you're using comparison (==
). Try this:
def bearing(x):
for i in range(len(x)):
while x[i]>=360:
x[i]-=360
if x[i]>270:
x[i]=360-x[i]
elif x[i]>180:
x[i]-=180
elif x[i]>90:
x[i]=180-x[i]
print (x)
You may also shorten the while loop into a single % 360
.
This is a cleaner function that does the same:
def bearing(x):
for i, v in enumerate(x):
v = v % 360
if v > 270:
v = 360-v
elif v > 180:
v -= 180
elif v > 90:
v = 180-v
x[i] = v
print (x)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments