Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 18th, 2013, 11:04 AM
dkoleary dkoleary is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 11 dkoleary User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old February 18th, 2013, 11:28 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is online now
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,379 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
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!

Reply With Quote
  #3  
Old February 18th, 2013, 02:24 PM
dkoleary dkoleary is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 11 dkoleary User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Stuck on simple python prob

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap