Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question
#1
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?
deanhystad write Jan-20-2024, 08:40 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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().
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020