I'm working on building a simple python program for a class which will run on a Raspberry Pi and an Arduino to direct a telescope. I had started to learn python some time ago, and I'm having trouble getting my functions to work properly. Right now, I have this:
import ephem
def const(p, d): # find the constellation #
def loc():
sbend = ephem.Observer()
sbend.lat = '41.67'
sbend.lon = '86.26'
p = getattr(ephem, p)
p.compute(sbend)
print p.alt, p.az
o = getattr(ephem, p)
print ephem.constellation(o(d))
return loc()
const(raw_input('Planet: '), raw_input('yyyy/mm/dd: '))
From what I remember, a function inside another can call a variable from the parent. Can it also work the other way round like I have at the end? I'd like to be able to print the constellation (which is working) as well as the alt and az of the planet based on the hardcoded location. For some reason, it isn't calculating the altitude and azimuth though. Thoughts?
EDIT
I added return loc()
on line 14.
I was doing more reading and some other threads said that to get to an inner function, it needs to be returned at the end of the parent. But, it's still not working for me.
loc()
but I cannot see that you use it anywhere. So before I go further: is there any code that you accidentally omitted when you pasted this code in? – Brandon Rhodes Oct 9 at 12:07return loc
— what you are doing instead, by sayingreturn loc()
, is you are first callingloc()
yourself to get a return value (which will beNone
, sinceloc()
includes noreturn
statement), then you are returning thatNone
value as the return value ofconst()
. – Brandon Rhodes Oct 9 at 13:24