Python Forum

Full Version: Question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I am Arlind Sherifi. I have just started to learn Python and I have written this code:

import numpy as np

perihelion = 70
velocity_perihelion = 80


def velocity_aphelion(perihelion, velocity_perihelion):
    star_mass = 1.9891e30
    G = 6.6738e-11
    second_term_1 = (2 * G * star_mass) / (velocity_perihelion)
    second_term_2 = -4 * (
        velocity_perihelion**2 - ((2 * G * star_mass) / (perihelion))
    )
    v2 = (second_term_1 - np.sqrt(second_term_1**2 - second_term_2)) / 2
    return v2


print(velocity_aphelion(perihelion, velocity_perihelion))


def aphelion(perihelion, velocity_perihelion, velocity_aphelion):
    L2 = (perihelion * velocity_perihelion) / velocity_aphelion
    return L2


print(aphelion(perihelion, velocity_perihelion, velocity_aphelion))


def Semimajor_axis(perihelion, aphelion):
    a = (1 / 2) * (perihelion + aphelion)
    return a


def Semiminor_axis(perihelion, aphelion):
    b = np.sqrt(abs(perihelion * aphelion))
    return b


def Orbital_period(s_major, s_minor, perihelion, velocity_perihelion):
    T = (2 * np.pi * s_major * s_minor) / (perihelion * velocity_perihelion)
    return T


def Orbital_eccentricity(perihelion, aphelion):
    e = (aphelion - perihelion) / (aphelion + perihelion)
    return e


def Planetary_orbits(perihelion, velocity_perihelion):
    v2 = float(velocity_aphelion(perihelion, velocity_perihelion))
    L2 = float(aphelion(perihelion, velocity_perihelion, v2))
    s_major = float(Semimajor_axis(perihelion, L2))
    s_minor = float(Semiminor_axis(perihelion, L2))
    period = float(Orbital_period(s_major, s_minor, perihelion, velocity_perihelion))
    eccentricity = float(Orbital_eccentricity(perihelion, L2))
    return Planetary_orbits


print(
    "aphelion: %f \n velocity at aphelion: %f \n orbital period: %f \\n eccentricity: %f."
    % (L2, v2, T, e)
)
This is the traceback File ~\exercise2.6.py:21 in aphelion
Error:
L2=(perihelion*velocity_perihelion)/velocity_aphelion TypeError: unsupported operand type(s) for /: 'int' and 'function'
Any way how to correct the code, please?
When you call aphelion() here, you pass velocity_aphelion as the third argument. velocity_aphelion is a function.
print(aphelion(perihelion, velocity_perihelion, velocity_aphelion))
Did you mean to pass velocity_perihelion instead? Or did you intend to pass the value calculated here:
print(velocity_aphelion(perihelion, velocity_perihelion))
If the latter, you'll need to assign the return value of velocity_aphelion() to a variable and pass that variable as the third argument to aphelion().