[Tutor] Temperature Scales

Alan Gauld alan.gauld at btinternet.com
Mon Nov 29 10:27:02 CET 2010


"Andre Jeyarajan" <andrejeyarajan at rogers.com> wrote

> Write two functions that will convert temperatures
> back and forth from the Celsius and Fahrenheit temperature
> scales (using raw_input)

If we ignore the raw_input bit you have done what you were asked.

> def C_F(x):
>    y = (x-32)*(5.0/9)
>    print y

Although as a stylistic point it is better to return values from
functions rather than print the values inside the functions.
You can then print the result of the function like this:

print C_F(x)

> def F_C(x):
>     y = (x*9.0/5)+32
>     print y

> I have created the two functions but I don’t know what to do from 
> here.

I suspect the assignment expects you to provide some code
that uses them, to show they work. Given the raw_input hint I'd
surmise they want you to ask the user for a temperature in C or F
and use your functions to print the corresponding temperature
in the other scale.

You will need to convert the raw_input value to an integer for
the functions to work. Can you do that?

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list