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))
3
  • 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 Commented Aug 29, 2016 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. Commented Aug 29, 2016 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. Commented Aug 29, 2016 at 21:19

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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
1

In this calculation:

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

You are not converting your result to str.

Also, you have spelled "Fahrenheit" incorrectly.

4 Comments

That isnt the line of code its erroring on? also I know i spelled it wrong.
That isnt the line of code its erroring on Perhaps you'd care to let us know what line of code it is erroring on?
Its in my code above.. + 'Kelvin: ' + str((Temp+ 273.15)) ## I get the error here ##
Ah, now I see. My bad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.