<bdo id='TGpya'></bdo><ul id='TGpya'></ul>
<tfoot id='TGpya'></tfoot>

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

      <legend id='TGpya'><style id='TGpya'><dir id='TGpya'><q id='TGpya'></q></dir></style></legend>

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

      1. 如何检查输入是否是 Python 中的数字?

        How do I check if input is a number in Python?(如何检查输入是否是 Python 中的数字?)
            <tbody id='xdaXu'></tbody>

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

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

                <bdo id='xdaXu'></bdo><ul id='xdaXu'></ul>
                <legend id='xdaXu'><style id='xdaXu'><dir id='xdaXu'><q id='xdaXu'></q></dir></style></legend>
                  <i id='xdaXu'><tr id='xdaXu'><dt id='xdaXu'><q id='xdaXu'><span id='xdaXu'><b id='xdaXu'><form id='xdaXu'><ins id='xdaXu'></ins><ul id='xdaXu'></ul><sub id='xdaXu'></sub></form><legend id='xdaXu'></legend><bdo id='xdaXu'><pre id='xdaXu'><center id='xdaXu'></center></pre></bdo></b><th id='xdaXu'></th></span></q></dt></tr></i><div id='xdaXu'><tfoot id='xdaXu'></tfoot><dl id='xdaXu'><fieldset id='xdaXu'></fieldset></dl></div>
                • 本文介绍了如何检查输入是否是 Python 中的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 Python 脚本,可以将十进制数转换为二进制数,这显然使用了他们的输入.

                  I have a Python script which converts a decimal number into a binary one and this obviously uses their input.

                  我想让脚本验证输入是一个数字,而不是其他任何会停止脚本的东西.

                  I would like to have the script validate that the input is a number and not anything else which will stop the script.

                  我尝试了 if/else 语句,但我真的不知道该怎么做.我试过 if decimal.isint():if decimal.isalpha(): 但当我输入字符串时它们只会抛出错误.

                  I have tried an if/else statement but I don't really know how to go about it. I have tried if decimal.isint(): and if decimal.isalpha(): but they just throw up errors when I enter a string.

                  print("Welcome to the Decimal to Binary converter!")
                  while True:
                      print("Type a decimal number you wish to convert:")
                      decimal = int(input())
                      if decimal.isint():
                          binary = bin(decimal)[2:]
                          print(binary)
                      else:
                          print("Please enter a number.")
                  

                  没有 if/else 语句,代码可以正常工作并完成其工作.

                  Without the if/else statement, the code works just fine and does its job.

                  推荐答案

                  如果int()调用成功,decimal已经数字.您只能在字符串上调用 .isdigit() (正确的名称):

                  If the int() call succeeded, decimal is already a number. You can only call .isdigit() (the correct name) on a string:

                  decimal = input()
                  if decimal.isdigit():
                      decimal = int(decimal)
                  

                  另一种方法是使用异常处理;如果抛出 ValueError,则输入不是数字:

                  The alternative is to use exception handling; if a ValueError is thrown, the input was not a number:

                  while True:
                      print("Type a decimal number you wish to convert:")
                      try:
                          decimal = int(input())
                      except ValueError:
                          print("Please enter a number.")
                          continue
                  
                      binary = bin(decimal)[2:]
                  

                  除了使用 bin() 函数并删除起始 0b,您还可以使用 format()函数,使用'b'格式,将整数格式化为二进制字符串, 没有前导文本:

                  Instead of using the bin() function and removing the starting 0b, you could also use the format() function, using the 'b' format, to format an integer as a binary string, without the leading text:

                  >>> format(10, 'b')
                  '1010'
                  

                  format() 函数可以轻松添加前导零:

                  The format() function makes it easy to add leading zeros:

                  >>> format(10, '08b')
                  '00001010'
                  

                  这篇关于如何检查输入是否是 Python 中的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  env: python: No such file or directory(env: python: 没有这样的文件或目录)
                  How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                  Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)
                  Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                  Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                  When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)

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

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

                            <tbody id='B2Jkk'></tbody>