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

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

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

          <bdo id='YWx4Y'></bdo><ul id='YWx4Y'></ul>
      1. “无法将'int'对象隐式转换为str"错误

        时间:2023-09-14
        <legend id='fie0l'><style id='fie0l'><dir id='fie0l'><q id='fie0l'></q></dir></style></legend>

              <tbody id='fie0l'></tbody>
            <tfoot id='fie0l'></tfoot>

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

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

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

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

                  问题描述

                  我正在尝试测试某个数字的十进制表示是否至少包含两次数字 9,所以我决定这样做:

                  I am trying to test if the decimal representation of a certain number contains the digit 9 at least twice, so I decided to do something like that:

                  i=98759102
                  string=str(i)
                  if '9' in string.replace(9, '', 1): print("y")
                  else: print("n")
                  

                  但 Python 总是以TypeError: Can't convert 'int' object to str implicitly"响应.

                  But Python always responds with "TypeError: Can't convert 'int' object to str implicitly".

                  我在这里做错了什么?是否有更智能的方法来检测某个数字在整数的十进制表示中包含的频率?

                  What am I doing wrong here? Is there actually a smarter method to detect how often a certain digit is contained in the decimal representation of an integer?

                  推荐答案

                  你的问题在这里:

                  string.replace(9, '', 1)
                  

                  您需要将 9 设为字符串文字,而不是整数:

                  You need to make 9 a string literal, rather than an integer:

                  string.replace('9', '', 1)
                  

                  至于计算字符串中 9 出现次数的更好方法,请使用 str.count():

                  As for a better way to count the occurrences of 9 in your string, use str.count():

                  >>> i = 98759102
                  >>> string = str(i)
                  >>> 
                  >>> if string.count('9') > 2:
                      print('yes')
                  else:
                      print('no')
                  
                  
                  no
                  >>>
                  

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

                  上一篇:通过 os.system 推送 下一篇:在 Python 中将字符串转换为枚举

                  相关文章

                  最新文章

                  <small id='3VO3g'></small><noframes id='3VO3g'>

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

                      <bdo id='3VO3g'></bdo><ul id='3VO3g'></ul>