Python Physics Class Project

Link to the lesson: https://www.codecademy.com/courses/learn-python-3/projects/physics-class

Hi team, I had a question. For this project, we did not have to define any variables though we used them without a problem. Even though we didn’t assign number values to train_mass, train_acceleration, or train_distance for example, we still got a number value as a result when we ran the function. Is this just because the lesson was created that way or are these just universally known units like inches and centimeters?

can you show an example of what you mean?

For these formulas, the unit of measurement can vary but with restrictions. Fictive example, if we use kilogram for mass, we have to use meters for length. If we grams for mass, then we have to use centimetres for length.

From a programming perspective, you should add comments to clarify the units of measurement.

Sure.
def get_energy(mass, c):
return mass * c ** 2

bomb_energy = get_energy(bomb_mass, c)

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

Here, though we use the variables bomb_mass and bomb_energy, they were never given numerical values. When this function is run in the project, however, it outputs a numerical value. I also linked the project on the original comment if that helps.

here:

bomb_energy = get_energy(bomb_mass, c)

you must have defined bomb_mass and c somewhere? Otherwise, you would get an error. Very likely you gave these variable a numeric value

1 Like

bomb_mass = 1
Is at the top of the screen in the script.py window under train_mass, train_acceleration, etc.

And, we define what `bomb_energy is in the function,

def get_energy(mass, c=3*10**8):
  return mass * c ** 2
bomb_energy = get_energy(bomb_mass)
1 Like

So as long as bomb_mass is defined, everything else can run based off that variable?

It depends what you meant by everything. You’ll still need other names/variables if you used them (including train_mass, train_acceleration etc.) but as for bomb_mass itself you can pass it around freely until it is delted or the name reassigned-

bomb_mass = 3
casing_mass = 110
total_mass = casing_mass + bomb_mass
print(total_mass)  # 113

def get_energy(mass, c=int(3e8)):
    return mass * c * c


# Passing it as an argument to a function is no different-
print(get_energy(bomb_mass))  # 270000000000000000
print(bomb_mass)  # 3
2 Likes

Oh, okay. Thank you @tgrtim, that clears things up a lot!