Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
February 18th, 2013, 11:04 AM
 dkoleary
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.

#2
February 18th, 2013, 11:28 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 3,379
Time spent in forums: 1 Month 2 Weeks 3 Days 13 h 3 m 20 sec
Reputation Power: 383
5/9 is zero in python2.
float(5)/9 is not zero in python2
Code:
```#!/usr/bin/python

from __future__ import division  ############ use this

def c2f(n):
"""Converts Celsius to Farenhieht"""
tf=(9/5 * n) + 32
return tf

def f2c(n):
"""Converts Farenheit to Celsius"""
tc = (5/9) * (n-32)  ## problem seems to be here
return tc

T = 'abc'
while not T.isdigit():
T = raw_input("Enter a temperature: ")

T = float(T) # avoid duplicate code

C = 'abc'
while C.upper() not in 'CF':
C = raw_input("Convert to (F)ahrenheit or (C)elsius: ")

if C.upper() == 'C':
n = f2c(T)
else:
n = c2f(T)

print "Converted: %3d" % (n,)```
__________________
[code]Code tags[/code] are essential for python code!

#3
February 18th, 2013, 02:24 PM
 dkoleary
Registered User

Join Date: Feb 2013
Posts: 11
Time spent in forums: 4 h 45 m 47 sec
Reputation Power: 0
Quote:
 Originally Posted by b49P23TIvg 5/9 is zero in python2. float(5)/9 is not zero in python2

Ah, so typecasting. eh, it's a form of conversion so I wasn't far off, I guess.

Thanks very much for the pointer. Hopefully, I won't run face first into that too many more times before it sinks in.

Doug

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Stuck on simple python prob