0

I am writing a temperature converter for fun and everything seems to be working except I am getting a 'TypeError: Can't convert 'float' object to str implicitly' message. From what I can tell I am converting it to a string after the fact, can anyone tell me what I am doing wrong?

I looked this this question previously "Can't convert 'float' object to str implicitly"

def tempconverter(startType,Temp):
    #conversion types: c -> f c-> K , f -> c, f -> k, k -> c, k -> f
    # maybe have them select only their beginning type then show
    # all temp types by that temperature

if startType[0].lower() == 'c':
    return  ('Your temperature ' + Temp +'\n'
            + 'Farenheight: ' + ((int(Temp)*9)/5)+32 + '\n'
            + 'Kelvin: ' + str((Temp+ 273.15)) ## I get the error here ##
            )
elif startType[0].lower() == 'f':
    #commented out until first is fixed
    #return  ((int(Temp)-32)*5)/9
    return Temp
elif startType[0].lower() == 'k':
    return  Temp

print('Welcome to our temperature converter program.')
print('''Please enter your temperature type:
         C = Celsius
         F = Farenheight
         K = Kelvin''')
sType = input('> ')

print('Please enter the temperature you wish to convert.')
sTemp = input('> ')

print(tempconverter(sType,sTemp))
Community
  • 1
  • 1
Taku_
  • 1,505
  • 3
  • 14
  • 22
  • You should use %s and %i escapes for your formatting. Looks cleaner and I bet will make your problem easier to trouble shoot if not fix it – Angus Aug 29 '16 at 21:12
  • 1
    @Angus: I wouldn't recommend that; the `%i` auto-rounding tends to cause more confusion than it's worth. I'd recommend sticking to the `format` method, or `%s` for everything. – user2357112 Aug 29 '16 at 21:16
  • I will look into using string formatting, I probably should have been using that but this way seemed less complicated? I dont really have an excuse to not do it. – Taku_ Aug 29 '16 at 21:19

2 Answers2

2

In the line of code:

+ 'Kelvin: ' + str((Temp+ 273.15)) ## I get the error here ##

The error is caused by this part of it:

Temp+ 273.15

Temp is a string. You can't add a string and a number together.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • I tried int() conversion before and it didnt work.. dammit Oh well Thanks! that fixed it. Now I am going to convert this to string formatting – Taku_ Aug 29 '16 at 21:34
1

In this calculation:

((int(Temp)*9)/5)+32

You are not converting your result to str.

Also, you have spelled "Fahrenheit" incorrectly.

kindall
  • 178,883
  • 35
  • 278
  • 309