-2

I wrote this code to convert geodetic coordinates to cartesian. It asks for user input and it prints x, y and z. The code runs without any errors, but after putting the inputs, the print function does not show the value of x.

What would be the issue here?

Thank you for your help.

import math

a = 6378137                     
f= 0.00335281068
latitude = math.radians(float(input('Enter Latitude:')))
longitude = math.radians(float(input('Enter Longitude:')))
height  = float(input('Enter Height:'))

def earthConverter(latitude, longitude, height):
    N = a / math.sqrt(1-e**2 * math.sin(longitude)**2)
    e = math.sqrt((2 * f) - (f**2))
    x = (N + height) * math.cos(longitude) * math.cos(latitude)
    y = (N + height) * math.cos(longitude) * math.sin(latitude)
    z = (N * (1 - (e**2) ) + height) * math.sin(latitude)
    return x, y, z

    earthConverter(123.0256, 56.45648, 21322.4545)
    print('x is %f' % x)
Zoe
  • 27,060
  • 21
  • 118
  • 148
A Banitaba
  • 19
  • 8
  • Possible duplicate of [How can I return two values from a function in Python?](https://stackoverflow.com/questions/9752958/how-can-i-return-two-values-from-a-function-in-python) – StefanS Jan 19 '19 at 08:56
  • Remember that Python is indentation-sensitive. That means your [mcve] needs to be properly indented. – Some programmer dude Jan 19 '19 at 08:57
  • As for your problem, your function returns the values, but you never assign the returned values to any variables. Variables in Python are *scoped*, the variables `x`, `y` and `z` that you define in the function are local to that function only. – Some programmer dude Jan 19 '19 at 08:58

1 Answers1

1

It seems that the intention of your function was not copied correctly. Because the code will not work as shown above. Seems to be a copy&paste error.

To fix the issue you need to store your result returned from the function:

def earthConverter(latitude, longitude, height):
    ...
    return x, y, z

x,y,z = earthConverter(123.0256, 56.45648, 21322.4545)
print('x is %f' % x)
KimKulling
  • 2,654
  • 1
  • 15
  • 26