<tfoot id='LUOXt'></tfoot>

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

        • <bdo id='LUOXt'></bdo><ul id='LUOXt'></ul>

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

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

        Python:TypeError:无法连接“str"和“int"对象

        时间:2023-09-11
        <tfoot id='WzLMq'></tfoot>

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

          <tbody id='WzLMq'></tbody>

          <legend id='WzLMq'><style id='WzLMq'><dir id='WzLMq'><q id='WzLMq'></q></dir></style></legend>
                  <bdo id='WzLMq'></bdo><ul id='WzLMq'></ul>

                  <i id='WzLMq'><tr id='WzLMq'><dt id='WzLMq'><q id='WzLMq'><span id='WzLMq'><b id='WzLMq'><form id='WzLMq'><ins id='WzLMq'></ins><ul id='WzLMq'></ul><sub id='WzLMq'></sub></form><legend id='WzLMq'></legend><bdo id='WzLMq'><pre id='WzLMq'><center id='WzLMq'></center></pre></bdo></b><th id='WzLMq'></th></span></q></dt></tr></i><div id='WzLMq'><tfoot id='WzLMq'></tfoot><dl id='WzLMq'><fieldset id='WzLMq'></fieldset></dl></div>
                • 本文介绍了Python:TypeError:无法连接“str"和“int"对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有这个将字符串添加到整数的 python 程序:

                  I have this python program that adds strings to integers:

                  a = raw_input("Enter a: ")
                  b = raw_input("Enter b: ")
                  print "a + b as strings: " + a + b
                  a = int(a)
                  b = int(b)
                  c = a + b
                  str(c)
                  print "a + b as integers: " + c
                  

                  我收到此错误:

                  Python: TypeError: cannot concatenate 'str' and 'int' objects
                  

                  如何将字符串添加到整数?

                  How can I add strings to integers?

                  推荐答案

                  有两种方法可以解决最后一个 print 语句引起的问题.

                  There are two ways to fix the problem which is caused by the last print statement.

                  您可以将 str(c) 调用的结果分配给 c,如 @jamylak 正确显示的那样,然后连接所有字符串,或者您可以替换最后 print 简单地用这个:

                  You can assign the result of the str(c) call to c as correctly shown by @jamylak and then concatenate all of the strings, or you can replace the last print simply with this:

                  print "a + b as integers: ", c  # note the comma here
                  

                  在这种情况下

                  str(c)
                  

                  不是必须的,可以删除.

                  isn't necessary and can be deleted.

                  样本运行的输出:

                  Enter a: 3
                  Enter b: 7
                  a + b as strings:  37
                  a + b as integers:  10
                  

                  与:

                  a = raw_input("Enter a: ")
                  b = raw_input("Enter b: ")
                  print "a + b as strings: " + a + b  # + everywhere is ok since all are strings
                  a = int(a)
                  b = int(b)
                  c = a + b
                  print "a + b as integers: ", c
                  

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

                  上一篇:有没有办法将数字单词转换为整数? 下一篇:TypeError:无法将“int"对象隐式转换为str

                  相关文章

                  最新文章

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

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

                  1. <tfoot id='qC22I'></tfoot>

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