#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    11
    Rep 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. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    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 and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0
    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

IMN logo majestic logo threadwatch logo seochat tools logo