Python: Functions. Script is not working

I did the last exercise from Learn Python: Functions and I am not sure if it correct.

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 ):
  c = 3*10**8
  return mass*c
  bomb_energy = get_energy(bomb_mass, c)
  print("A 1 lg bomb supplies" + str(bomb_energy) + "Joules. ")
def get_work(mass, accelertion, distance):
  force = get_force(mass, acceleration) * distance
  return
  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.")

https://www.codecademy.com/paths/computer-science/tracks/cspath-intro/modules/cspath-python-functions/projects/physics-class

Please format your code.

How is your script not working? (I can’t test it because it’s not formatted.) Is it giving you errors? Is it just not producing the expected result?

2 Likes

I have edited the post so that the code is formatted.

No, I don’t think that it is.

First, though, a quick note for the future. More so than in some other languages, formatting - especially indentation - is very important in Python. When you’re posting code to the forum, please use the code formatting tool. All you need to do is look for the </> icon in the menu bar which appears at the top of the text box while you type.

capture

Press Enter to go to a blank line, click that icon, and you will see this:

capture_r

Just copy and paste your code directly from the editor into that highlighted portion, and all your lovely formatting, indentation etc, will be kept.

Now, on to the issue at hand.

You have a couple of errors:

  1. You’ve neglected to provide the entire program. The project provides you with several variables up front, such as train_mass. Whilst I can access the project, and so can see what these variables are, not everyone here who might try and help you is a Pro user. It helps if we have all the information, which means posting everything from the editor please. :slight_smile:

  2. Missing variables aside, you have some incorrect indentation. For example, you have the following:

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.")

The lines after your return statement should not be indented - or, more accurately, each line should be as far left as the line def get_force...

  1. Your get_work function doesn’t return anything. :slight_smile:

That should give you some fixes to be getting on with. :slight_smile: Once you’ve fixed that, your code shouldn’t be too far off. :slight_smile:

3 Likes

Thanks you for this cool and helpful advieces :upside_down_face: (I’ve never used any forum :sweat_smile:)

Everything you say is understandable to me, but one thing surprised me. Nameerror appears for train_mass (which is defined in script.py - maybe I got it wrong?) which you mentioned. :thinking:

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.")