26

I am having trouble 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 comes up as the following:

    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 am unsure what the problem is as i have changed the name from temp to temperature and added the brackets around temperature but still the error occurs.

Wolf
  • 9,679
  • 7
  • 62
  • 108
Eps12 Gaming
  • 295
  • 1
  • 4
  • 6

3 Answers3

63

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

cast it to str

PYA
  • 8,096
  • 3
  • 21
  • 38
  • Thanks, that worked – Eps12 Gaming Jul 05 '17 at 03:44
  • please accept the answer :) – PYA Jul 05 '17 at 03:45
  • Why is this the case? Why doesn't it just call int's __str__() magic method and convert it before embedding into the string? – Danil Jul 06 '18 at 04:49
  • @Danil not particularly sure - i'll have to dig in to that – PYA Jul 07 '18 at 17:02
  • @Danil python does not usually automatically coerce things ("Explicit is better than implicit" - Zen of Python), when using the string's concatenate operator `+` it expects 2 strings and will fail if it doesn't get 2 strings. Note: `print()` takes multiple arguments and automatically calls `__str__()` on those arguments which is why you can do, `print("the furnace is now ", temperature, "degrees!")` without the explicit conversion. – AChampion Oct 28 '18 at 05:06
18

Python comes with numerous ways of formatting strings:

New style .format(), which supports a rich formatting mini-language:

>>> 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 the new f"" format strings:

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

Or using print()s default separator:

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

And least effectively, construct a new string by casting it to a str() and concatenating:

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

Or join()ing it:

>>> print(' '.join(["the furnace is now", str(temperature), "degrees!"]))
the furnace is now 10 degrees!
AChampion
  • 29,683
  • 4
  • 59
  • 75
7

you need to cast int to str before concatenating. for that use str(temperature). Or you can print the same output using , if you don't want to convert like this.

print("the furnace is now",temperature , "degrees!")
badiya
  • 2,247
  • 13
  • 23