Codecademy: physics

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility
https://www.codecademy.com/courses/learn-python-3/projects/physics-class
When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!


I have followed the advice in the video but instead of getting an output of 32.0 I get a blank screen. Would anyone have any idea if there is a breakdown in my logic or syntax? Thanks in advance. :slightly_smiling_face: :slightly_smiling_face:
If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

Can you tell us more about your logic, in your own words?

1 Like

Check how much you indented your code.

The line

f100_in_celcius = f_to_c(100) 

should not be indented because it it not part of the f_to_c function.

Similarly,
the lines

c0_in_fahrenheit = c_to_f(0)  
print(c0_in_fahrenheit)  

should not be indented because those are not part of the function definition for the function before these lines, function c_to_f

The task is to convert temperatures between Fahrenheit and Celsius using the mathematical formulas given in the instructions. While I can more or less understand the code given being able to my own logic to the task given is an issue.

The indentation advice given by @janbazant1107978602 would be a good start to visualizing the behavior of your code. This is critical in Python since that is the only way it has of identifying code blocks.

As an aside, while learning and adopting syntax rules, keep also in mind the readability of your code. A simple rule to follow is to always surround operators with whitespace.

Eg.

(f_temp - 32) * 5 / 9

You, and all your readers will appreciate clearly seeing all the operations.

put more whitespace between the parts of the code it make it easier to read? Makes sense.

1 Like

https://www.codecademy.com/courses/learn-python-3/projects/physics-class

I have managed to isolate to the problem to lines 15 & 16. When I run the code the error message will say c-temp undefined. I take that t mean that there is no value assigned to c-temp for the IDE to recognize? If that is the problem I am unsure of how to assign c-temp a value to make the program execute lines 15 & 16. Does anybody else have any ideas it would be much


appreciated.

I think it’s a capitalization issue.
You seem to have:

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

but the names of the variable don’t match
because one is c_temp and the other is C_temp
(The c has to be uppercase in both, or it should be lowercase in both so that its the same every time.)

https://www.codecademy.com/courses/learn-python-3/projects/physics-class

you were absolutely right that was the problem in lines 14 & 5

I think I have a solution finally and thanks to the contributions from this forum. Hopefully one day I can return the favor.

1 Like

Codecademy Project: Physics Class Sample Code Below

start of sample code

Uncomment this when you reach the “Use the Force” section

train_mass = 22680

train_acceleration = 10

train_distance = 100

bomb_mass = 1

Write your code below:

converting farenheit to celsius

def f_to_c (f_temp):

return(f_temp -32) *5 / 9

f100_in_celsius = f_to_c(100)

converting celsius to farenheit

def c_to_f (c_temp):

return(c_temp * 9 / 5) + 32

c0_in_fahrenheit = c_to_f(0)

print(c0_in_fahrenheit)

calculating train force

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

end of sample code

Hey there!

When sharing code, please make sure you format it according to this guide:
https://discuss.codecademy.com/t/how-do-i-format-code-in-my-posts/28351/6
It’s especially important for languages like Python that are very sensitive to things like indentation, as the forums software won’t retain any of this if the code is just pasted in.

Happy coding!