Skip to content

Instantly share code, notes, and snippets.

@manucabral
Created April 27, 2022 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manucabral/32a48bdc28a377357d1fef0292d2b688 to your computer and use it in GitHub Desktop.
Save manucabral/32a48bdc28a377357d1fef0292d2b688 to your computer and use it in GitHub Desktop.
Calculate the gravitational force between two objects using the Newton's law of gravitation.
# Universal Gravitational Constant
G = 6.67408e-11
def get_force(**kwargs):
"""
Calculate the gravitational force between two objects using the Newton's law of gravitation.
Params:
first_mass: mass of the first object (kg)
first_radius: radius of the first object (kg)
distance: distance between the two objects (m)
Returns:
force: gravitational force between the two objects (N)
Raises:
ValueError: if not arguments are passed
ValueError: if the distance is less than zero
"""
if not kwargs:
raise ValueError('No arguments provided')
first_mass = kwargs.get('first_mass', 0)
second_mass = kwargs.get('second_mass', 0)
distance = kwargs.get('distance', 1)
if distance < 0:
raise ValueError('Distance must be greater than zero')
force = G * first_mass * second_mass / distance ** 2
return force
if __name__ == '__main__':
result = get_force(first_mass=100, second_mass=100, distance=1)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment