How to change value inside function

quesadyllan

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:

  • If it's above 270, it will subtract the number from 360.
  • If it's above 180, it will subtract 180 from the number.
  • If it's above 90, it will subtract the number from 180.

-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.

Bharel

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to change the value of sum function inside the query?

From Dev

Change the value of a variable inside a function

From Dev

How to change the value of data inside a struct through a function?

From Dev

How to change the value of data inside a struct through a function?

From Dev

How do I change the value of any global variable inside of a function?

From Dev

call function on change of value inside <p> tag

From Dev

Change step value inside range function?

From Dev

how to get a value inside a function

From Dev

How can i change the value of a variable inside a function in node.js?

From Dev

How can I change user inside function

From Dev

How to change a variable declare inside a function in javascript

From Dev

How to prevent value change inside $watch

From Dev

How to change step value inside the loop?

From Dev

How to change value of property inside data attribute

From Dev

How to change the default value of a function with another function?

From Dev

Change global variable value with a function from inside window.onload

From Dev

jQuery function to change td value with button inside td

From Dev

How to apply a function to a value inside a constructor in haskell?

From Dev

How to get a value inside a clickHandler function jQuery

From Dev

How to calculate the mean value of the column inside the function

From Dev

How to repeat a function with change some variables inside of the function? PYTHON 3.4

From Dev

Android how to change value of a Key/Value Pair inside JSONObject

From Dev

How to execute a function when a value change?

From Dev

How to change the value of a variable that is declared out of a function?

From Dev

Handsontable : how to change cell value in render function

From Dev

How to change function return value with decorators in python?

From Dev

How to change Bool value within a function in javascript?

From Dev

VBA : How to change the cell's value in a function?

From Dev

How to change the value of variable in a function in javascript?

Related Related

  1. 1

    How to change the value of sum function inside the query?

  2. 2

    Change the value of a variable inside a function

  3. 3

    How to change the value of data inside a struct through a function?

  4. 4

    How to change the value of data inside a struct through a function?

  5. 5

    How do I change the value of any global variable inside of a function?

  6. 6

    call function on change of value inside <p> tag

  7. 7

    Change step value inside range function?

  8. 8

    how to get a value inside a function

  9. 9

    How can i change the value of a variable inside a function in node.js?

  10. 10

    How can I change user inside function

  11. 11

    How to change a variable declare inside a function in javascript

  12. 12

    How to prevent value change inside $watch

  13. 13

    How to change step value inside the loop?

  14. 14

    How to change value of property inside data attribute

  15. 15

    How to change the default value of a function with another function?

  16. 16

    Change global variable value with a function from inside window.onload

  17. 17

    jQuery function to change td value with button inside td

  18. 18

    How to apply a function to a value inside a constructor in haskell?

  19. 19

    How to get a value inside a clickHandler function jQuery

  20. 20

    How to calculate the mean value of the column inside the function

  21. 21

    How to repeat a function with change some variables inside of the function? PYTHON 3.4

  22. 22

    Android how to change value of a Key/Value Pair inside JSONObject

  23. 23

    How to execute a function when a value change?

  24. 24

    How to change the value of a variable that is declared out of a function?

  25. 25

    Handsontable : how to change cell value in render function

  26. 26

    How to change function return value with decorators in python?

  27. 27

    How to change Bool value within a function in javascript?

  28. 28

    VBA : How to change the cell's value in a function?

  29. 29

    How to change the value of variable in a function in javascript?

HotTag

Archive