954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

python exercise- function to return temp conversion

"""
Write a function to convert temperature from Celsius to Fahrenheit scale.
oC to oF Conversion: Multipy by 9, then divide by 5, then add 32.

Examples

>>> Cel2Fah(28.0)
'82.40'
>>> Cel2Fah(0.00)
'32.00'
"""
so it says, but i couldn't think of an a way to do so efficiently. But none the less, i felt i should try it and then look up a better way after
the code i wrote is as follows;

def Cel2Fah(temp):
	Fah = temp * 9 / 5 + 32
	Fah = float(Fah)
	Fah_string = str(Fah)
        a = Fah_string.split('.')
        b = a[1][0:2]
        if len(b) < 2:
        	b = b + '0'
        elif len(b) > 2:
        	b = float(b)
        	b = round(b)
        	b = str(b)
                b = b[0:2]
        c = a[0]
        f = str(c) + '.' + str(b)
        return f


could someone show be a better way? it would be much appreciated. i have spent a lot of time reading documentation, but when i go to put it into practice its messy like this ( most of the time), please help me advance my knowledge.

pwolf
Junior Poster in Training
71 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 
def Cel2Fah(temp):
	Fah = temp * 9. / 5 + 32
	Fah_str = '%.2f' % Fah
	return Fah_str
pwolf
Junior Poster in Training
71 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 
def Cel2Fah(celcius):
	return '%.2f' % (celcius * 9. / 5 + 32)
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: