
February 18th, 2013, 11:04 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 11
Time spent in forums: 4 h 45 m 47 sec
Reputation Power: 0
|
|
|
Stuck on simple python prob
Hey;
I'm a recoverying perl programmer trying to pick up python as part of my current gig. I went out and found some 'practice' problems on the web which don't supply answers. Theoretically, shouldn't need 'em, this one should be simple, but it's got me stumped.
Short version: temperature conversion between celsius and farenheight
Here's the code:
Code:
#!/usr/bin/python
def c2f(n):
"""Converts Celsius to Farenhieht"""
tf=(9/5 * n) + 32
return tf
def f2c(n):
"""Converts Farenheit to Celsius"""
print "N: " + n
tc = (5/9) * (int(n)-32) ## problem seems to be here
print "TC: %5.2f" % (tc,)
return tc
T = 'abc'
while not T.isdigit():
T = raw_input("Enter a temperature: ")
C = 'abc'
while C.upper() not in ['C', 'F']:
C = raw_input("Convert to (F)arenheight or (C)elsius: ")
if C.upper() == 'C':
n = f2c(T)
else:
n = c2f(T)
print "Converted: %3d" % (n,)
When I run it, I get:
Code:
$ ./temp_converter.py
Enter a temperature: 80
Convert to (F)arenheight or (C)elsius: c
N: 80
TC: 0.00
I'm suspecting some type of integer/string/float conversion issue; however, I've tried all three in the print statement to no avail.
I know the c2f function is borked; I figure I can get that fixed once I figure out what's up with the f2c.
I've apparently missed some fairly basic concept. Can someone point it out to me?
Thanks.
|