Taku_
Taku_

Reputation: 1625

Python Float to String TypeError

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))

Upvotes: 0

Views: 140

Answers (2)

John Gordon
John Gordon

Reputation: 33275

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.

Upvotes: 2

kindall
kindall

Reputation: 184081

In this calculation:

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

You are not converting your result to str.

Also, you have spelled "Fahrenheit" incorrectly.

Upvotes: 1

Related Questions