 Learn python 3 Physics Class

Lesson

Hi I am doing this class and I have followed the instructions and then watched the first part of the video that comes with the course.

The requirement is -

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 .

Part two requires me to -

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.

This is what I have written

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

f100_in_celsius = f_to_c(100)

print(f100_in_celsius)

This has given me the correct result however, I have not been able to It "return c_temp " as required in the first part of the question. I watched the video and it makes no mention of this `c_temp’.

Can you please explain how I should correectly return `c_temp’.

2 Likes

c_temp is just a variable in which you store the result of the conversion:

c_temp = (f_temp - 32) * 5/9

before returning the result:

return c_temp
1 Like

Ok so moving to the next step… I am trying to understand the application. I get an error stating that the code is wrong but I am not fully sure how.

a Return should return a value back to the function if I am right so I am trying to see the process tree for that function.

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

f100_in_celsius = f_to_c(100)

f_to_c() missing 1 required positional argument: ‘c_temp’ is the error thrown.

Two positional arguments expected.

Yeah it is the comedy of that, the two arg’s aren’t required. The second due to the return in pointless.

The exercise nowhere specifies a function requiring two parameters. They only ask you to define two functions,

f_to_c(f_temp) and c_to_f(c_temp)

… and test them with
f100_in_celsius = f_to_c(100) and c0_in_fahrenheit = c_to_f(0)

then

print(f100_in_celsius)
print(c0_in_fahrenheit)

# Should output
37.77777777777778
32.0

Yes,

I recieved the output when changed but due tot he “return” passback the actual function still require two inputs as the def has two outputs. This being said the second input is a dummy variable if written as a call.

I still don’t see why you feel there is a need for two parameters.

Is this your function (from an earlier post)?

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

If so, there is no need for c_temp in the parameter list.
This works fine:

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

The variable c_temp that you name and assign within the function requires no parameter to justify its existence. It is a garden-variety function variable, lives its entire life within the function, and is “seen” nowhere outside of the function.

The only thing that is actually “returned” is a reference to the memory address where the calculated value is stored. The name c_temp is not attached to it in any way.

def c_to_f(c_temp, f_temp):
f_temp = (c_temp * (9/5) + 32)
return f_temp

c0_in_fahrenheit = c_to_f(37.0)
print(c0_in_fahrenheit

versus

def c_to_f(c_temp, f_temp):
f_temp = (c_temp * (9/5) + 32)
return f_temp

c0_in_fahrenheit = c_to_f(37.0, 0)
print(c0_in_fahrenheit

The first of the two throws a code

Traceback (most recent call last):
File “script.py”, line 18, in
c0_in_fahrenheit = c_to_f(37.0)
TypeError: c_to_f() missing 1 required positional argument: ‘f_temp’

As mentioned above, we do not supply a second parameter to the function. Only the variable which value we are converting.

>>> def c_to_f(C):
return 9 * C / 5 + 32

>>> c_to_f(-40)
-40.0
>>> def f_to_c(F):
return 5 * (F - 32) / 9

>>> f_to_c(-40)
-40.0
>>>

im working on this one now, how do i test my code to see if its right or not? all it does is save

Are there any results displaying in the right pane (the console)?