Problem with Python physics class exercise

Hi all this is my first post here. I am having trouble in that the script.py doesn’t match the lesson I am doing. I will try uploading a screenshot where you will see that the lesson is about writing a function to convert temperature from F to C but script.py is all about a future lesson regarding a Train mass and a bomb. I have tried using the refresh at the bottom of script.py and also I have refreshed Firefox which has helped before but not this lesson. Thanks in advance for any suggestions,

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

Hi, @tera8187553565. Just follow the instructions. You will be adding to the provided script as you progress, and will eventually get to the train mass and bomb.

2 Likes

Thanks patrickd314 I will give it a go. It looks like I need a blank sheet for this lesson. I hope next lesson it resets back to normal. Cheers.

I was confused with this one at first too. It already had data on it and the first line was asking about temperature? What the heck. But it worked out. You end up using that data at the top for some stuff in the bottom like @patrickd314 said.

1 Like

Thanks for that wickdw8yz now that you have pointed that out I see it further down. I deleted the train wreck and did the first two exercises but I couldn’t test them because I only had a save button not a print button. what I will do now is cut those two exercises then reset the page and hopefully it will return the train wreck (I have it saved to a word document) then I will paste the two exercises back above the train wreck.

Hi all I wonder if someone could check my two exercises here because I don’t have a print button only a save button. I will paste the questions below. I will paste in the link and upload a screen shot with the exercises on it. They are def f_to_c and def c_to_f I am hoping I have the indents at the bottom correct. Thanks for any input
https://www.codecademy.com/paths/computer-science/tracks/cspath-intro/modules/cspath-python-functions/projects/physics-class

Turn up the Temperature

Write a function called f_to_c that takes an input f_temp , a temperature in Fahrenheit, and converts it to c_temp , that temperature in Celsius.

It should then return c_temp .

The equation you should use is:

Temp © = (Temp (F) - 32) * 5/9

Let’s test your function with a value of 100 Fahrenheit.

Define a variable f100_in_celsius and set it equal to the value of f_to_c with 100 as an input.

Write a function called c_to_f that takes an input c_temp , a temperature in Celsius, and converts it to f_temp , that temperature in Fahrenheit.

It should then return f_temp .

The equation you should use is:

Temp (F) = Temp © * (9/5) + 32

Let’s test your function with a value of 0 Celsius.

Define a variable c0_in_fahrenheit and set it equal to the value of c_to_f with 0 as an input.

You can print() the value of c0_in_fahrenheit to check that it equals 32.0 .

After your you define your function, next you run the parameter of that function.

def f_to_c (ftemp):
return(ftemp-32) * 5/9

then it wants you to define the new functions parameters

f100_in_celsius = f_to_c(100)

followed by printing that statement

print(f100_in_celsius)

Oh, and the print() indention will be lined up with your

def f_to_c
I hope that helps and you can use it as the example to continue to the celsius to fahrenheit.

1 Like

@tera8187553565, In a sense, it all comes down to indentation.

The key here is the distinction between a function and a function call. I’ll use one of your examples. Note that I’ve changed the indentation of your line 4, and added a new line:

def f_to_c(f_temp):
  c_temp = (f_temp -32) * 5/9
  return c_temp

f_100_in_celsius = f_to_c(100)
print(f_100_in_celsius)
# prints 37.77777777777778
  • The three lines beginning with def are the function, f_to_c(), which has one parameter, f_temp.

  • The parameter says to the function: "When you are called, there will be a number at this position (in this case, it’s the only position) within the parentheses. Where you first see the variable name f_temp in your function body, substitute that number.

  • When the Python interpreter Runs a script, it moves down along the leftmost indentation level, and at each statement found there, does what it is told. When it sees that “def”, it stores the underlying function as an object somewhere in memory, saves the function name f_to_c in short-term memory(the “stack”) for reference, and then moves on.

  • The next thing the interpreter gets to is the line f_100_in_celsius = f_to_c(100). This is an assignment statement (you can tell by the assignment operator, = ). It does two things:

  1. Evaluates the expression on the right side of the assignment operator, to obtain a value
  2. Assigns that value to the variable on the left side of the assignment operator.
  • So, per step 1, evaluate the expression on the right side, so we come (finally!) to the function call, f_to_c(100). It says to Python, “Find that function f_to_c that you stored in memory, and go through all of its steps, remembering to assign the value 100 (the argument) to the first instance of the variable, f_temp (the parameter.)”

  • That process yields the value 37.777…, which is returned to the calling statement, then assigned to the variable f_100_in_celsius.

  • And here, in your version, that process stops. The variable f_100_in_celsius has the value 37.777… but you will never know that, because the variable is never used. That is why I added the line print(f_100_in_celsius), which becomes the next line that the interpreter gets to.

One last point: That function call itself behaves like a variable. It can be used, for instance in an expression just as if it were a value, without any intermediate variable assignment:

twice_temp = 2 * f_to_c(100)
print(twice_temp)
# prints 75.55555555555556
1 Like

Thanks for that wickdw8yz it’s not easy when there is no print option. I am looking at installing Python on my computer that way I can run the exercises and see how it goes. There is nothing like making a few mistakes to learn from. Cheers.

Remember that you always have a print option. Just call print(something). That will be no different if you are running Python locally or in the Codecademy editor.

I’m all for making use of a local IDE, by the way, and suggest that you give Thonny a try.

1 Like