I am trying to convert the distance of Jupiter which is in AU (Astronomical Unit) to Lightyears, I am using preexisting modules which means you have no preference over the data types and i am having the following error.
I am using the modules Skyfield (Skyfield.api to be specific) and Scipy My code:
from scipy import constants
from skyfield.api import load
planets = load('de421.bsp')
earth, jupiter = planets['earth'], planets['JUPITER BARYCENTER']
ts = load.timescale()
t = ts.now()
astrometric = earth.at(t).observe(jupiter)
radec=astrometric.radec()
# int(constants.astronomical_unit / constants.light_year ) * int(str(radec[2])
# Since the above line is not working i tried this:
int(constants.astronomical_unit / constants.light_year ) * int(str(radec[2]).replace("au", "").strip())
Error:
int(constants.light_year / constants.astronomical_unit) * int(str(radec[2]).replace("au", "")) ValueError: invalid literal for int() with base 10: '4.63954'
I at first thought the whitespace might be the reason but even when i applied the strip()
function the error persists still
my Python version is Python 3.9.12
CodePudding user response:
Try using int(radec[2].au)
instead of int(str(radec[2]))
so it becomes:
int(constants.light_year / constants.astronomical_unit) * int(radec[2].au))
If you print it you get 252964
.
Note: You should consider making all the calculations on floats and at the end, convert the answer to int:
print(int(float(constants.light_year / constants.astronomical_unit) * float(radec[2].au)))
Which gives 293505
.
CodePudding user response:
Try converting the string value of radec[2] to a float before converting it to an integer
int(constants.light_year / constants.astronomical_unit) *
int(float(str(radec[2]).replace("au", "").strip()))