= 1: p...">

Python TypeError must be str not int

I am having problems with the following piece of code:

    if verb == "stoke":

        if items["furnace"] >= 1:
            print("going to stoke the furnace")

            if items["coal"] >= 1:
                print("successful!")
                temperature += 250 
                print("the furnace is now " + (temperature) + "degrees!")
                           ^this line is where the issue is occuring
            else:
                print("you can't")

        else:
            print("you have nothing to stoke")

      

The resulting error occurs like this:

    Traceback(most recent call last):
       File "C:\Users\User\Documents\Python\smelting game 0.3.1 build 
       incomplete.py"
     , line 227, in <module>
         print("the furnace is now " + (temperature) + "degrees!")
    TypeError: must be str, not int

      

I'm not sure if the problem is that I changed the name from temp to temperature and added parentheses around the temperature, but still an error occurred.

+6


source to share


3 answers


print("the furnace is now " + str(temperature) + "degrees!")



enter str

+25


source


Python comes with numerous ways to format strings:

New style .format()

that supports rich formatting minilanguage:

>>> temperature = 10
>>> print("the furnace is now {} degrees!".format(temperature))
the furnace is now 10 degrees!

      

%

Old style format specifier:

>>> print("the furnace is now %d degrees!" % temperature)
the furnace is now 10 degrees!

      

In Py 3.6 using newline format f""

:

>>> print(f"the furnace is now {temperature} degrees!")
the furnace is now 10 degrees!

      



Or using the print()

sep

default arator:

>>> print("the furnace is now", temperature, "degrees!")
the furnace is now 10 degrees!

      

And the least efficient way to create a new string is by casting it to str()

and concatenating:

>>> print("the furnace is now " + str(temperature) + " degrees!")
the furnace is now 10 degrees!

      

Or join()

to him:

>>> print(' '.join(["the furnace is now", str(temperature), "degrees!"]))
the furnace is now 10 degrees!

      

+11


source


you need to pass int to str before concatenation. use for this str(temperature)

. Or you can print the same result with ,

if you don't want to convert it.

print("the furnace is now",temperature , "degrees!")

      

+2


source







All Articles