Python Forum
def functions and floats
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
def functions and floats
#1
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.
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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()
Reply
#4
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Round off floats in a list nagymusic 10 4,774 Apr-02-2019, 12:43 AM
Last Post: nagymusic
  I need help with floats and int calloflegend11 4 3,787 Aug-27-2017, 10:29 PM
Last Post: BerlingSwe

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020