Python: TypeError: Cannot concatenate 'str' and 'int'

后端 未结 2 1198
既然无缘
既然无缘 2021-01-17 04:35

I\'m starting with some basic stuff to get a feeling for how to make a text based game. After creating the .py file in my IDE, I open terminal and use bash to open the .py

相关标签:
2条回答
  • 2021-01-17 04:50

    You can do your mathematical calculation and convert it into string, for example

    *a = x*3
    str(a)*
    

    and now if you call it in your print statement, it will work.

    a = x*3
    str(a)
    print("3 x {} = {} ").format(x,a)
    
    0 讨论(0)
  • 2021-01-17 04:53

    I'm not entirely sure (I have never coded with Python before) but it seems that the hpGanon variable is an integer and not a string, so it cannot put the two together. To convert an integer to a string in Python (I searched this up :P), you need to do str(integer).

    So, to implement this in your example, try something like this:

    print("Ganon now has " + str(hpGanon) + "hit points.")

    0 讨论(0)
提交回复
热议问题