Hello everybody. I recently started a python temperature conversion program and seem to be stuck. Im fairly new so I tried several things such as a fuction to convert temperature and and if and else statement and both combined. My code lets me input a temperature but then doesn't convert it. My code is below, any help would be great!Thanks!

#Clesisus and Farenheit converter
import math
print('Hello.What is your name?')
myName=input()
print('Ok,+ myName +, Would you like to convert to celsisus or farenheit?')
answer=input()

if answer=='Celsius' or answer=='celsius':
    print('Please enter the degree in farenheit in which you wish to convert.')
    degree=input()
    degree='Farenheit'
def covert_temperature(Farenheit):
    return ((Farenheit-32)*5/9==celsius)
convert_temperature


if answer=='Farenheit' or answer=='farenheit':
    print('Please enter the degree in celsius in which you wish to convert.')

Recommended Answers

All 15 Replies

Start bye testing your convert function,it's not working.

def covert_temperature(fahrenheit):
    '''Convert fahrenheit to celsius'''
    return (fahrenheit - 32) * 5.0/9.0

Test:

>>> covert_temperature(1)
-17.22222222222222
>>> covert_temperature(50)
10.0

So working correct.
== Checks if the value of two operands are equal or not.
You can not have it your function and "celsius" has no meaning in function.

Thanks for the help.I appreciate it.

When I input this code above in, it works when I manually call the function. With farenheit as the input of this fuction wouldn't it automatically input it and calcualte it or do i need to somehow include it by surronding it like so "+ word+"? like you do when it inputs your name into the sentence at the begining of my code.

in line 5 it should be
print('ok,'+ MyName+'.........')
in line 11
Farenheit = float(degree)
in line 12: 'n' missing in convert
line 14:
print(convert_temperature(Farenheit))
it is a good practice to include your def at the begining of the code

What is happening here is that you are defining the function at the point where you want to be calling it, but you never actually call it. You do refer to the function, on line 14 of the code above, but that is not a function call; without parameters, it simply returns a reference to the function itself. In order to call the function, you need to have an argument list, where to values to be passed to the function are in parentheses, like this:

convert_Fahr2Cel(degree)

This is true even of functions that don't take parameters; those would need an empty argument list, like so: foo(). Without the argument list, you are returning the function, not the function's value.

You have a number of other errors in the code as it is, as well. First off, you generally want to separate the function from where it is being called; the usual practice is to put the functions before the running code, and then have the actual code in a test to make sure that the source file is the one that should be running, like so:

if __name == '__main__':
    # your running code here

What this is doing is checking that this file is being used as a program, and not a module (a source file that is called from another program). Don't worry too much about it yet, it will make sense later when you learn about modules, just know that it is a good practice to do it this way.

The next problem I see is that you have the return value of the function defined as

return ((Farenheit-32)*5/9==celsius)

Unfortunately, this doesn't work as you seem to expect. the equality operator, ==, tests to see if two values are the same, and returns a boolean value based on that. Thus, the function would return either True or False, rather than the value you wanted. Moreover, since the variable celsius isn't defined inside the function, it doesn't have a value to compare. You want to just have the conversion to Celsius there.

Finally, when you do get the value of the function, you want to do something with it! In this case, you want to print it. The following code show what your program should have loked like:

#Celsius and Fahrenheit converter
import math

def convert_Fahr2Cel(Fahrenheit):
    '''
    Takes a temperature value in degrees Celsius and returns 
    the equivalent in degrees Fahrenheit.
    '''
    return (((Fahrenheit - 32) * 5) / 9)

if __name == '__main__':
    print('Hello.What is your name?')
    myName=input()
    print('Ok,+ myName +, Would you like to convert to Celsius or Fahrenheit?')
    answer=input()

    if answer=='Celsius' or answer=='celsius':
        print('Please enter the degree in Fahrenheit in which you wish to convert.')
        degree=input()
        print(convert_Fahr2Cel(degree))

    if answer=='Fahrenheit' or answer=='fahrenheit':
        print('Please enter the degree in Celsius in which you wish to convert.')
commented: Nice. +15

Okay thanks. Im playing around with where the two functions should go and it is saying that theres unsupported operand types where i return the result after the function at the beginning of the code. Going to mess around with it to get it to work.

You don't have to import the math module.
Follow Scol_R_LEA's advice, it will make your code more orderly and easier to read. It will be easier to get help.

I tried to follow his advice but the code didnt seem to work.

if I may , I would like to point out the typing mistakes in Schol-R-LEA code :
line 11 should be :
if name == 'main':
in line 19 , make degree either float or int because input() is string:
degree=float(input())
and your own mistake on line 14 , it does not work as you expect it , you should add a "'" after ok, so it should read :
print('ok,' + myName + 'would you like to convert to Celsius or Fahrenheit?')
it should work now
good luck

line 11 does not show what I typed , it should be 2 underscores before and after name and main

Okay thank you.What is the point of making it name==main though,just wondering. and do i have to put that degree=float for all of my inputs and do i put it as a line after it says degree=input()?

to answer your first question , snippsat has a good explanation , check this out http://www.daniweb.com/software-development/python/threads/452884/please-help-me-to-understand-this-afraid-to-use-without-proper-knowledges#post1964536

now to answer the second question : when you use the function input(), whatever the user types is treated as a string so if I ask
age=input('how old are you ?') and you type 27 , this is a string '27' , not a number so to change that to a number you type either
age = int(input('how old are you '))
or
age = input('how old are you ?')
age = int(age)
use int() or float() depending on your program needs
some people preffer to use function eval() with function input() when they expect the user to type an expression ( a number , a list , etc)
like this:
age = eval(input('how old are you ?')) and your answer , 27 , is a number
well , it is about 12 midnight and my brain is sleep , I hope my explanation has been helpful, other people with higher number of posts can probably be more helpful
good night

Completed code below.Thank you for the help. I am learning alot more now and am going to focus on functions as those seemed to be my weakpoint in this code.

#Celsisus and Farenheit converter

def convert_celsius(Degree):
    return (((Degree - 32) * 5.0) / 9.0)

def convert_Farenheit(Degree):
    return (( 9.0 / 5.0) * Degree + 32)

print('Hello.What is your name?')
myName=input()
print('Ok,'+ myName +', Would you like to convert to celsius or farenheit?')
answer=input()

if answer=='Celsius' or answer=='celsius':
    print('Please enter the Degree in Farenheit in which you wish to convert.')
    Degree=input()
    Degree=int(Degree)
    print(convert_celsius(Degree))


if answer=='Farenheit' or answer=='farenheit':
    print('Please enter the Degree in celsius in which you wish to convert.')
    Degree=input()
    Degree=int(Degree)
    print(convert_Farenheit(Degree))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.