Physics project function - no syntax error but no console print-out either?

On the Getting Ready for Physics class step one, I tried to get a printout of the Fahrenheit to Celsius conversion with the following code:

def f_to_c(f_temp = 350):
temp_c = (f_temp - 32) * 5/9
return temp_c
print(str(f_temp) + " degrees Farenheit converts to " + str(temp_c) + " degrees Celsius.")

f_to_c()

I’m not getting any syntax errors but I’m not getting anything printed out on the console either. What am I missing here, please?

Also, while we are at it, is there a particular reason that the returned value of f_to_c is then saved to a new variable in Step 2?

The code works for me.

Formatted:

def f_to_c(f_temp = 350):
    temp_c = (f_temp - 32) * 5/9
    return temp_c
    
    print(str(f_temp) + " degrees Farenheit converts to " + str(temp_c) + " degrees Celsius.")

f_to_c()

>>>176.66666666666666

Did you try changing the order of return and print()? (Unless you just want to return the temperature conversion(?)).
I don’t think the question asks for a keyword argument, just positional so you can call the function with any argument (temperature).

Summary
def f_to_c(f_temp):
    temp_c = (f_temp - 32) * 5/9    
    print(str(f_temp) + " degrees Farenheit converts to " + str(temp_c) + " degrees Celsius.")
    return temp_c

f_to_c(400)
>>>>400 degrees Farenheit converts to 204.44444444444446 degrees Celsius.
204.44444444444446

Also, going forward please format your code.

Thanks re: how to format code!

I know it didn’t ask for keyword; I was just trying some stuff out. BUT I don’t know why it would work for you but not for me. At least it makes sense the way I did it? :woman_shrugging:

Did you expand the “Summary” to view the edited code that I provided?

Also, I used an external IDE and not the learning environment to test the code. Sometimes in the LE, if you don’t have exactly what they’ve asked for, your code won’t work (pass the tests behind the scenes).
If you want to make changes to the code/beyond what’s asked, I recommend using an IDE or code editor (or, if you’re on a mac, Terminal).

Oh my goodness my jaw is on the floor. I didn’t catch the thing about putting the return statement after but that fixed it. My question is why was it necessary to move it to after the print statement? If you can offer an explanation without too much trouble I’d appreciate it. Thank you for your time!

1 Like

the question doesn’t ask one to print anything. Just return a value to the caller.

The return statement immediately terminates the function, and returns the value back. So anything placed after that inside the function isn’t recognized (like your original print()) . So, then you move it before the return. In your code, the item returned when the function is called is temp_c.

def f_to_c(f_temp): #parameter, f_temp
    temp_c = (f_temp - 32) * 5/9    
    print(str(f_temp) + " degrees Farenheit converts to " + str(temp_c) + " degrees Celsius.")
    return temp_c #this value is returned when you call the function.

f_to_c(400) #calling the function and passing an argument "through" it.

It’s a good habit to get into–to move pieces of code around to see how it/they act–if stuff throws an error or not to really try to understand what it’s doing.

More reading:

2 Likes
print(str(f_temp) + " degrees Farenheit converts to " + str(temp_c) + " degrees Celsius.")

Poking my head in here with a shout out to the folks who were responsible for implementing the most recent string formatting method: f-string. That one improvement added to workflow more than any recent revisions to the language. It certainly eliminated a slough of typos!

print (f"{f_temp} degrees Farenheit converts to {temp_c} degrees Celsius.")

Now that one is aware of this string formatting method it becomes pointless to think of string concatenation, ever again.

1 Like

Thank you so much! :bouquet:

1 Like

Oh yeah! Cool. Thanks. I thought there was a less cluttered way to incorporate the variables but I couldn’t remember what it was!

1 Like