Exception TypeError: must be str, not int in Python 3

Dung Do Tien Apr 24 2022 162

Hello, I just joined a course about learning Python on Codecademy. I wrote a small function as below:

 import numpy as np

def sumable(a ,b) :
  return a + b
  
msg = "Total your working days are: "
totalWorkingDay = sumable(10, 16)

print(str(msg + totalWorkingDay))

I want to learn about function, datatype, input & output in python. But When I was running the code above and got an exception TypeError: must be str, not int.

 Traceback (most recent call last):
  File "main.py", line 10, in <module>
    print(str(msg + totalWorkingDay))
TypeError: must be str, not int

I know that str() function will help convert something datatype to string, but it didn't work for me.

I'm using Python 3.9 and Windows 11. Thanks for any idea.

Have 1 answer(s) found.
  • S

    Sornchai Sirichantharat Apr 24 2022

    I think you used str() function is correct but you're wrong way to use it.

    Please change:

     print(str(msg + totalWorkingDay))

    To

     print(msg + str(totalWorkingDay))

    It's will solve for you

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close