<small id='5mrvh'></small><noframes id='5mrvh'>

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

      1. <tfoot id='5mrvh'></tfoot>
          <bdo id='5mrvh'></bdo><ul id='5mrvh'></ul>

        如何在 Python 中使用 input 输入整数

        时间:2023-09-11

          <tfoot id='Fcl1D'></tfoot>

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

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

                  <tbody id='Fcl1D'></tbody>

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

                  问题描述

                  我正在尝试自学如何用 Python 编写代码,这是我第一次在 Stack Overflow 上发帖,所以请原谅本文中的任何不当之处.但是,让我们开始吧.

                  I'm trying to teach myself how to code in Python and this is my first time posting to Stack Overflow, so please excuse any improprieties in this post. But let's get right to it.

                  我正在尝试使用输入命令返回一个整数.我也进行了研究,以下是我在 Python 3.4 中的多次尝试以及以下结果:

                  I'm trying to use the input command to return an integer. I've done my research, too, so below are my multiple attempts in Python 3.4 and the results that follow:

                  guess_row = int(input("Guess Row: "))
                  

                  我得到以下信息:

                  Traceback (most recent call last):
                  File "<input>", line 1, in <module> 
                  ValueError: invalid literal for int() with base 10: 'Guess Row: 2`
                  

                  尝试 #2

                  guess_row = float(input("Guess Row: "))
                  

                  我得到以下信息:

                  Traceback (most recent call last):
                  File "<input>", line 1, in <module>
                  ValueError: could not convert string to float: "Guess Row: 2""
                  

                  尝试 #3

                  try:
                      guess_row=int(input("Guess Row: "))
                  except ValueError:
                      print("Not an integer")
                  

                  在这里,我得到以下信息:

                  Here, I get back the following:

                  Guess Row: 2
                  Not an integer
                  

                  虽然它返回了一些东西,但我知道这是错误的,因为一方面,输入作为字符串返回,它还返回打印命令.

                  Although it returns something, I know this is wrong because, for one, the input returns as a string and it also returns the print command.

                  重点是,我尝试过 int、float 和 try,但到目前为止没有任何效果.有什么建议?我只想能够输入一个整数并将其作为一个返回.

                  Point being, I've tried int, float, and try, and so far nothing has worked. Any suggestions? I just want to be able to input an integer and have it returned as one.

                  推荐答案

                  您的第三次尝试是正确的 - 但是在此代码之前/之后 guess_row 发生了什么?例如,考虑以下情况:

                  Your third attempt is correct - but what is happening to guess_row before/after this code? For example, consider the following:

                  a = "Hello"
                  try:
                      a = int(input("Enter a number: "))
                  except ValueError:
                      print("Not an integer value...")
                  print(str(a))
                  

                  如果您输入一个有效数字,最后一行将打印出您输入的值.如果没有,将引发异常(在 except 块中显示错误消息)并且 a 将保持不变,因此最后一行将打印Hello".

                  If you enter a valid number, the final line will print out the value you entered. If not, an exception will be raised (showing the error message in the except block) and a will remain unchanged, so the final line will print "Hello" instead.

                  您可以对此进行细化,以便无效数字提示用户重新输入值:

                  You can refine this so that an invalid number will prompt the user to re-enter the value:

                  a = None
                  while a is None:
                      try:
                          a = int(input("Enter a number: "))
                      except ValueError:
                          print("Not an integer value...")
                  print(str(a))
                  

                  这篇关于如何在 Python 中使用 input 输入整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何查找整数中的数字长度? 下一篇:为什么(0-6)是-6 = False?

                  相关文章

                  最新文章

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

                  1. <small id='4GkWm'></small><noframes id='4GkWm'>

                      <bdo id='4GkWm'></bdo><ul id='4GkWm'></ul>
                    1. <legend id='4GkWm'><style id='4GkWm'><dir id='4GkWm'><q id='4GkWm'></q></dir></style></legend>
                    2. <tfoot id='4GkWm'></tfoot>