Physics Class get_force(mass, acceleration)

Hi All,

I am working on Python 3 - Physics class project.
https://www.codecademy.com/courses/learn-python-3/projects/physics-class

Step 11 states:

Define a final function called get_work that takes in mass , acceleration , and distance .

Work is defined as force multiplied by distance. First, get the force using get_force , then multiply that by distance . Return the result.

I have attached a screen shot with 2 options:

Why does number 1 not work?
force = get_force * distance

The video tutorial states it must be:

force = get_force(mass, acceleration)
return force * distance

My question is - why does get_force need the (mass, acceleration) brackets and why cant it just be multiplied in one row?

Thanks,
Darren

Screen Shot 2020-04-03 at 11.06.16

get_force is a function. Calling a function requires parentheses.

You could indeed squash everything in one function:

def get_work(mass, acceleration, distance):
  return mass*acceleration * distance

Not ideal, but possible.

1 Like

Thanks for the reply.

My question pertains to this -force=get_force(mass, acceleration)

Does get_force not store mass * acceleration already from earlier on? Why do I have to re-iterate it.

Thanks,

can i see your full code and can you highlight the lines your question pertains to?

If i had to guess, i would say you have a function call somewhere which calls get_force. Which is fine.

But the get_work function calculates everything in one go, using get_force as helper function. Which is separate of any earlier function calls you made.

1 Like

Thank you.

def f_to_c(f_temp):
  return (f_temp - 32) * 5/9
  
f100_in_celsius = f_to_c(100)
print(f100_in_celsius)

def c_to_f(c_temp):
  return (c_temp * (9/5) + 32)

c0_in_fahrenheit = c_to_f(0)
print(c0_in_fahrenheit)


train_mass = 22680
train_acceleration = 10
train_distance = 100

bomb_mass = 1

def get_force(mass, acceleration):
  return mass * acceleration

train_force = get_force(train_mass, train_acceleration)

print(train_force)
print("The GE train supplies " + str(train_force) + " Newtons of force.")



def get_energy(mass, c=3*10**8):
  return mass * c**2

bomb_energy = get_energy(bomb_mass)

print("A 1kg bomb supplies " + str(bomb_energy) + " Joules.")

def get_work(mass, acceleration, distance):
  force = get_force(mass, acceleration)
  return force * distance

train_work = get_work(train_mass,train_acceleration, train_distance)

print("The GE train does " + str(train_work) + " Joules of work over " + str(train_distance) + " meters.")

if you want to use train_force, you would have to make your get_work function in such a way that it can handle different parameter amount:

def get_work(distance, *args):
   if len(args) == 1:
     return distance * args[0]
   return distance * get_force(args[0], args[1])

or something like that.

Thanks for the response, but Im not sure you understand my question - or probably I dont understand your answer as Im new to this.

Please could you read my question at the top of the page - it is from Python 3 (physics class) step 11.
Where it states:

Define a final function called get_work that takes in mass , acceleration , and distance .

Work is defined as force multiplied by distance. First, get the force using get_force , then multiply that by distance . Return the result.

In line two of my screenshot I tried typing get_force * distance

BUT watched a tutorial on this project and saw that I needed to type

force = getforce(mass, acceleration)
return force * distance

I wondered why get_force was not remembering from earlier on that it had already calculated mass * acceleration (under bomb_mass if you scroll up in my code)

Im just wanting to understand this… Maybe its not important

Thank you.

A function can be executed multiple times, so the only “remembering” that is happening is the result you store in the variable.

Which is why, if you want to use the result from earlier, you will have to tweak the function like i showed you (with args). I was already several steps ahead it seems

Discard the notion of anything remembering or doing what you want.

Things have specific behaviour which you can leverage to get what you want. If you expect something, then you better have some code which makes that happen.

get_force is a function.
distance is a number
You’re saying “multiply this function by this number”
that doesn’t much make sense. this is not going to do what you want, it’s going to do what you said.

So, if you mean to call that function, then, how do you call a function? Do that, that’s the thing it promises to do, that’s what you can leverage to get what you want.

Thank you for the assistance

Makes sense thank you

to response for your first problem,
in line 38, force = get_force * distance
get_force is a function so pair of parenthesis “()” is required with the function calling.
and also that it seems that your function get_force has 2 parameters so you have to put those inside parenthesis also.

1 Like

Thank you makes sense