Python Forum
Help with Tempature Conversion Program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Tempature Conversion Program
#1
Lightbulb 
I was given an assignment to traslate a program from javascript to Python (I don't know Javascript) and then to add in 2 while statements. I haven't made it very far into it yet, but each time I try to fix this program I keep getting the same error on line 9: "Line 9: TypeError: 'float' object is not callable" and I'm not sure how to fix it. I'm a beginner. Can anyone help me fix my error? Confused Huh
print("Tempture Conversion")
print("___________________")
f = float(input("Enter tempature in Farenhieght"))
con = float(input("Enter the desired tempature translation: "))
#this is a list of the options
print(" \n 1) Celcius \n 2) Kelvin \n 3) Rankine \n 4) Delisle \n 5) Newton \n 6) Reamur \n 7) Romer ")

#I was trying to do the math for the problems here

c = int((5/9)(f - 32))
k = int((f - 32) (5/9) + 273.15)
ra = int(k * 1.8)
de = int((212-f)*5/6)
N = int((f - 32)* 0.18333)
re = int((f - 32) / 2.25)
ro = int((f -32)*7/24+7.5)

#The while statement is supposed to repeat until the user types in "Stop" asking the same question in a loop cycle.

while( con != "Stop"):
    if(con == 1):
        print(" Tempature in Celcius: " + str(c))
    con = float(input("Enter the desired tempature translation:(Stop to stop) "))
Reply
#2
Which line is line 9 in your program? Above it is a blank line.

The error is associated with taking a float object and trying to use it like a function. As an example:

>>> f = 5.2   # This makes f a float object
>>> print(f)  # we can print f
5.2
>>> f()       # but we can't call f
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'float' object is not callable
It looks like you expect that con could at some point be "Stop". But if you're setting it this way:

con = float(input("Enter the desired tempature translation: "))
Then that isn't possible. You're casting the input value to a float. If someone typed in "stop", the float() conversion would fail and would generate an error.
Reply
#3
In lines 10 and 11 you use a math notation that does not work in programming. (5/9)(f-32) needs to have a * added to be
(5/9)*(f-32)
Same error in 11
Reply
#4
I like temperature conversion and I thought it would be fun to write a program in Python. I started with your program, but I didn't like that I had to enter temps in degrees Farenheight and I didn't like having to run the program over each time I wanted to enter a different temperature. I changed the program so the user can enter a temp using any of the units and use a more natural input like this:
Output:
Enter Temperature followed by units: 32 F 0.0 C 150.0 D 32.0 F 273.15 K 0.0 N 491.67 Ra 0.0 Re 7.5 Ro
This was surprisingly easy in Python. For conversion I put all of my equations in a dictionary so I could look them up by unit. I chose Celsius as my standard unit and I have a dictionary of equations to convert from various units to Celsius, and another to convert from Celsius to various units. After that all I had to do was loop, split the input into parts, convert to Celsius, and convert from Celsius to all the other units.
to_celsius = {
    'C'  : lambda C : C,
    'D'  : lambda D : 100 - D * 2 / 3,
    'F'  : lambda F : (F-32)/1.8,
    'K'  : lambda K : K - 273.15,
    'N'  : lambda N : N / 0.33,
    'RA' : lambda RA: (RA - 491.67) / 1.8,
    'RE' : lambda RE: RE * 1.25,
    'RO' : lambda RO: (RO - 7.5) * 40 / 21 }

from_celsius = {
    'C'  : lambda C: C,
    'D'  : lambda C: (100 - C) * 3 / 2,
    'F'  : lambda C: C * 1.8 + 32,
    'K'  : lambda C: C + 273.15,
    'N'  : lambda C: C * 0.33,
    'RA' : lambda C: C * 1.8 + 491.67,
    'RE' : lambda C: C / 1.25,
    'RO' : lambda C: C * 21 / 40 + 7.5 }

units = {'C':'Celsius', 'D':'Delisle', 'F':'Farenhieght', 'K':'Kelvin',
         'N':'Newton', 'Ra': 'Rankine', 'Re':'Reamur', 'Ro':'Romer' }

unit_str = ','.join([f'{sym}:{name}' for sym, name in units.items()])

print("Tempture Conversion\n___________________")

print('Enter temeprature followed by units.  For units use these codes:')
print(unit_str)

while True:
    inp = input('Enter Temperature followed by units: ').split()
    if len(inp) == 0:  ## Nothing entered.  Quit
        break;
    if len(inp) == 1:  ## No units?  Use Celsius as default
        inp.append('C')
    try:
        ## Convert to Celsius
        celsius = to_celsius[inp[1].upper()](float(inp[0]))

        ## Convert Celsius to all units
        for unit in units:
            print(from_celsius[unit.upper()](celsius), unit)

    ## Catch error and offer helpful suggestion
    except ValueError:
        print('Separate temperature and units by space')
    except KeyError:
        print('Use these temperature units\n', unit_str)
It would be pretty easy to convert something like this to one of those programs where you have two boxes for values and two drop-down lists for selecting units.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with conversion program uwl 1 1,006 Aug-19-2022, 09:08 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020