• <legend id='Y2eN3'><style id='Y2eN3'><dir id='Y2eN3'><q id='Y2eN3'></q></dir></style></legend>

    <small id='Y2eN3'></small><noframes id='Y2eN3'>

      • <bdo id='Y2eN3'></bdo><ul id='Y2eN3'></ul>
      <i id='Y2eN3'><tr id='Y2eN3'><dt id='Y2eN3'><q id='Y2eN3'><span id='Y2eN3'><b id='Y2eN3'><form id='Y2eN3'><ins id='Y2eN3'></ins><ul id='Y2eN3'></ul><sub id='Y2eN3'></sub></form><legend id='Y2eN3'></legend><bdo id='Y2eN3'><pre id='Y2eN3'><center id='Y2eN3'></center></pre></bdo></b><th id='Y2eN3'></th></span></q></dt></tr></i><div id='Y2eN3'><tfoot id='Y2eN3'></tfoot><dl id='Y2eN3'><fieldset id='Y2eN3'></fieldset></dl></div>

        <tfoot id='Y2eN3'></tfoot>

        TypeError:无法将“int"对象隐式转换为str

        时间:2023-09-11
        1. <legend id='bCHN4'><style id='bCHN4'><dir id='bCHN4'><q id='bCHN4'></q></dir></style></legend>

          <i id='bCHN4'><tr id='bCHN4'><dt id='bCHN4'><q id='bCHN4'><span id='bCHN4'><b id='bCHN4'><form id='bCHN4'><ins id='bCHN4'></ins><ul id='bCHN4'></ul><sub id='bCHN4'></sub></form><legend id='bCHN4'></legend><bdo id='bCHN4'><pre id='bCHN4'><center id='bCHN4'></center></pre></bdo></b><th id='bCHN4'></th></span></q></dt></tr></i><div id='bCHN4'><tfoot id='bCHN4'></tfoot><dl id='bCHN4'><fieldset id='bCHN4'></fieldset></dl></div>
            <tbody id='bCHN4'></tbody>

            1. <small id='bCHN4'></small><noframes id='bCHN4'>

                <bdo id='bCHN4'></bdo><ul id='bCHN4'></ul>
              • <tfoot id='bCHN4'></tfoot>

                  本文介绍了TypeError:无法将“int"对象隐式转换为str的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试编写一个文本游戏,但我在定义的函数中遇到了一个错误,该函数让你在制作角色后基本上可以花费你的技能点.起初,错误表明我试图在这部分代码中从整数中减去一个字符串:balance -strength.显然这是错误的,所以我用 strength = int(strength) 修复了它...但现在我遇到了这个我以前从未见过的错误(新程序员),我对它到底是什么感到困惑正试图告诉我以及我如何解决它.

                  I am trying to write a text game and I have run into an error in the function I am defining that lets you basically spend your skill points after you make your character. At first, the error stated that I was attempting to subtract a string from an integer in this part of the code:balance - strength. Obviously that was wrong so I fixed it with strength = int(strength)... but now I am getting this error which I have never seen before(new programmer) and I am stumped on what exactly it is trying to tell me and how I fix it.

                  这是我的代码,用于部分功能不起作用:

                  Here is my code for the part of the function that isn't working:

                  def attributeSelection():
                      balance = 25
                      print("Your SP balance is currently 25.")
                      strength = input("How much SP do you want to put into strength?")
                      strength = int(strength)
                      balanceAfterStrength = balance - strength
                      if balanceAfterStrength == 0:
                          print("Your SP balance is now 0.")
                          attributeConfirmation()
                      elif strength < 0:
                          print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!")
                          attributeSelection()
                      elif strength > balance:
                          print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!")
                          attributeSelection()
                      elif balanceAfterStrength > 0 and balanceAfterStrength < 26:
                          print("Ok. You're balance is now at " + balanceAfterStrength + " skill points.")
                      else:
                          print("That is an invalid input. Restarting attribute selection.")
                          attributeSelection()
                  

                  这是我在 shell 中访问这部分代码时遇到的错误:

                  And here is the error I get when I get to this part of the code in the shell:

                      Your SP balance is currently 25.
                  How much SP do you want to put into strength?5
                  Traceback (most recent call last):
                    File "C:Python32APOCALYPSE GAME LIBRARYapocalypseGame.py", line 205, in <module>
                      gender()
                    File "C:Python32APOCALYPSE GAME LIBRARYapocalypseGame.py", line 22, in gender
                      customizationMan()
                    File "C:Python32APOCALYPSE GAME LIBRARYapocalypseGame.py", line 54, in customizationMan
                      characterConfirmation()
                    File "C:Python32APOCALYPSE GAME LIBRARYapocalypseGame.py", line 93, in characterConfirmation
                      characterConfirmation()
                    File "C:Python32APOCALYPSE GAME LIBRARYapocalypseGame.py", line 85, in characterConfirmation
                      attributeSelection()
                    File "C:Python32APOCALYPSE GAME LIBRARYapocalypseGame.py", line 143, in attributeSelection
                      print("Ok. You're balance is now at " + balanceAfterStrength + " skill points.")
                  TypeError: Can't convert 'int' object to str implicitly
                  

                  有谁知道如何解决这个问题?先谢谢了.

                  Does anyone know how to solve this? Thanks ahead.

                  推荐答案

                  您不能将 stringint 连接.您需要使用 str 函数将 int 转换为 string,或使用 formatting 格式化输出.

                  You cannot concatenate a string with an int. You would need to convert your int to a string using the str function, or use formatting to format your output.

                  更改:-

                  print("Ok. Your balance is now at " + balanceAfterStrength + " skill points.")
                  

                  到:-

                  print("Ok. Your balance is now at {} skill points.".format(balanceAfterStrength))
                  

                  或:-

                  print("Ok. Your balance is now at " + str(balanceAfterStrength) + " skill points.")
                  

                  或根据评论,使用 , 将不同的字符串传递给您的 print 函数,而不是使用 + 连接:-

                  or as per the comment, use , to pass different strings to your print function, rather than concatenating using +: -

                  print("Ok. Your balance is now at ", balanceAfterStrength, " skill points.")
                  

                  这篇关于TypeError:无法将“int"对象隐式转换为str的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python:TypeError:无法连接“str"和“int"对象 下一篇:Python字符串和整数连接

                  相关文章

                  最新文章

                • <small id='M1atn'></small><noframes id='M1atn'>

                    <bdo id='M1atn'></bdo><ul id='M1atn'></ul>

                  <tfoot id='M1atn'></tfoot>

                  1. <legend id='M1atn'><style id='M1atn'><dir id='M1atn'><q id='M1atn'></q></dir></style></legend>

                  2. <i id='M1atn'><tr id='M1atn'><dt id='M1atn'><q id='M1atn'><span id='M1atn'><b id='M1atn'><form id='M1atn'><ins id='M1atn'></ins><ul id='M1atn'></ul><sub id='M1atn'></sub></form><legend id='M1atn'></legend><bdo id='M1atn'><pre id='M1atn'><center id='M1atn'></center></pre></bdo></b><th id='M1atn'></th></span></q></dt></tr></i><div id='M1atn'><tfoot id='M1atn'></tfoot><dl id='M1atn'><fieldset id='M1atn'></fieldset></dl></div>