• <bdo id='hif3P'></bdo><ul id='hif3P'></ul>
  • <small id='hif3P'></small><noframes id='hif3P'>

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

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

        在 Python 中使用 try/except 将字符串转换为 Int

        时间:2024-04-21

      1. <tfoot id='npAm9'></tfoot>
          <tbody id='npAm9'></tbody>

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

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

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

                  本文介绍了在 Python 中使用 try/except 将字符串转换为 Int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以我很困惑如何使用 try/except 函数将字符串转换为 int.有谁知道如何做到这一点的简单功能?我觉得我在字符串和整数上仍然有点朦胧.我非常有信心整数与数字有关.字符串...不是那么多.

                  So I'm pretty stumped on how to convert a string into an int using the try/except function. Does anyone know a simple function on how to do this? I feel like I'm still a little hazy on string and ints. I'm pretty confident that ints are related to numbers. Strings...not so much.

                  推荐答案

                  在使用 try/except 块时,具体说明您要捕获的异常非常重要.

                  It is important to be specific about what exception you're trying to catch when using a try/except block.

                  string = "abcd"
                  try:
                      string_int = int(string)
                      print(string_int)
                  except ValueError:
                      # Handle the exception
                      print('Please enter an integer')
                  

                  Try/Excepts 非常强大,因为如果某事可能以多种不同的方式失败,您可以指定您希望程序在每种失败情况下如何反应.

                  Try/Excepts are powerful because if something can fail in a number of different ways, you can specify how you want the program to react in each fail case.

                  这篇关于在 Python 中使用 try/except 将字符串转换为 Int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何将标准输出和标准错误重定向到 Python 中的记录器 下一篇:安排 Python 脚本每小时准确运行

                  相关文章

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

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

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