Python Forum

Full Version: def functions and floats
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
def celcius(c):
    c = 0
    return f=(1.8 * c) + 32 
    
print celcius()
Directions follow. I'm was just trying to get the first part before going on and I'm running into issues.

Write a function that takes one parameter - a float which represents a temperature in Celcius - and returns a float which represents that temperature in Fahrenheit.

Then, write a function that does the opposite conversion.

Here are the formulas for temperature conversion:
F=(1.8 x c) +32
C=(f-32) /1.8

Finally, write some code below your functions that
uses them to convert 0 degrees Celcius and 100 degrees
Celcius to Fahrenheit, and to convert 40 degrees
Fahrenheit and 80 degrees Fahrenheit to Celcius.
Make sure to print your results for the above
to the console.
there are number of errors in your code. Please, always post full traceback you get, in error tags... And we can work together to fix them all
I comment inside your code:
def celsius(c):
    # With the next line you are ignoring completely the input value and replacing it by 0
    c = 0
    # The next line is not valid python code, either define a variable f and return it
    # f = (1.8 * c) + 32
    # return f
    # Or return the result of an operation and do not create the variable f
    # return (1.8 * c) + 32
    return f=(1.8 * c) + 32

# You are learning python, using the print like this is python2 only.
# As you are learning, I recommend to use python3 and then the print command needs ():
# print(celsius())
print celsius()
Just to add to what killerrex already said - you need to supply some number as argument c when calling function, e.g.
print celsius(10.0), i.e. you want to convert 10.0 degrees cecsius

also, more appropriate name of the function is fahrenheit (or something like celsius2farenheit) because you convert Celsius to farenheit, not the other way around

also instead of one char argument c, better use something descriptive like celcius