Physics class project

well last print line is not working , NameError: name ‘train_work’ is not defined i failed to know where is the issue or the mistake i made i need help

train_mass = 22680

train_acceleration = 10

train_distance = 100

bomb_mass = 1

def f_to_c(f_temp) :

return (f_temp - 32 * 5/9)

f100_in_celsius = f_to_c(100)

def c_to_f (c_temp) :

return (c_temp * (9/5) + 32)

c0_in_fahrenheit = c_to_f(0)

def get_force (mass , acceleration) :

return ( mass * acceleration)

train_force = get_force (train_mass , train_acceleration)

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

Hello @ruby6036400210, welcome to the forms! Could you please post the formatted code? To format code, press this button:


and put the code between the back ticks:
Screen Shot 2020-05-02 at 14.06.13

1 Like

Yes, it’s hard to know where the mistake is. The format button looks like </>, you have to put the text you want to format in between the ‘’’

From your code and your error message, it seems that train_work is not defined globally. This could happen if it is indented improperly (and the compiler interprets it to be part of a function).

But again, indentation is key in python, so without knowing how it’s indented we’ll only be able to help so much.

2 Likes

@ruby6036400210 I tried to edit your original post to correctly format the code, but it looks like it wasn’t originally pasted in with the indentation of the original program.

You should be able to see how I’ve done the formatting, so please re-post your code with the correct formatting and we’ll try and help. (Alternatively, this post explains how to do it.)

:slight_smile:

thanks for your info here is the code below


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

Hi @ruby6036400210 you can alternatively format your code using markdown. For a single line of code, use backticks ( ` ) which is below the esc key on a US keyboard, for multi-line code, use triple backticks. Remember these go in a set so you need to close them just like open and closed inverted commas.

You may also refer to this article for the specifics of markdown.

1 Like

thanks for helping :slight_smile:

no worries @ruby6036400210

Hi @ruby6036400210:

Could you try rerunning your code again? I was able to do so locally on IDLE without any errors.

Remember to indent your code :smile: !

1 Like

:smiley: really appreciate i ve been checking indent on code for ever it kept gives me and error but when i tried now finally it works thanks a bunch :smiley:

1 Like

No worries! Glad I could help!

1 Like