Ace
Ace

Reputation: 1

Temperature calculator isn't outputting correct temperature?

I've tried to make a temperature conversion calculator in python. Any help with what's wrong? I put in 20C, and it's telling me that 20c=52f, which I know isn't correct. Here's the code:

def convert(changeto,temp):
    if changeto == "c":
        converted = (5/9)*(temp-32)
        print '%d C =  %d F' % (temp,converted)
    elif changeto == "f":
        converted = (9/5)*(temp+32)
        print '%d F = %d C' % (temp, converted)
    else:
        print "Error, type C or F for Celsius or Fahrenheit conversions."

print "Temperature Converter"

temp = float(raw_input("Enter a temperature: "))
changeto = str(raw_input("Convert to (F)ahrenheit or (C)elsius? "))

convert(changeto,temp)

raw_input("Any key to exit")

Upvotes: 0

Views: 100

Answers (1)

NPE
NPE

Reputation: 500267

Depending on your version of Python, 5/9 probably evaluates to zero. Change it to 5./9 (the dot turns the 5 into a floating-point literal).

The same goes for the other division.

On top of that, the two formulae you have are not inverses of one another and need to be re-examined.

Upvotes: 3

Related Questions