你如何在 Python 中使用 input 输入整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26692611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you input integers using input in Python
提问by The_Last_Halibut
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 编写代码,这是我第一次在 Stack Overflow 上发帖,所以请原谅这篇文章中的任何不当之处。但让我们开始吧。
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:
我正在尝试使用输入命令返回一个整数。我也做了我的研究,所以下面是我在 Python 3.4 中的多次尝试以及以下结果:
Attempt #1
尝试 #1
guess_row = int(input("Guess Row: "))
I get back the following:
我得到以下信息:
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'Guess Row: 2`
Attempt #2
尝试#2
guess_row = float(input("Guess Row: "))
I get back the following:
我得到以下信息:
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: could not convert string to float: "Guess Row: 2""
Attempt #3
尝试 #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.
虽然它返回了一些东西,但我知道这是错误的,因为一方面,输入作为字符串返回,它还返回打印命令。
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.
重点是,我已经尝试过 int、float 和 try,但到目前为止没有任何效果。有什么建议?我只想能够输入一个整数并将其作为一个返回。
回答by GoBusto
Your third attempt is correct - but what is happening to guess_rowbefore/after this code? For example, consider the following:
您的第三次尝试是正确的 - 但是guess_row此代码之前/之后发生了什么?例如,请考虑以下情况:
a = "Hello"
try:
a = int(input("Enter a number: "))
except ValueError:
print("Not an integer value...")
print(str(a))
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 exceptblock) and awill remain unchanged, so the final line will print "Hello" instead.
如果输入有效数字,最后一行将打印出您输入的值。如果没有,将引发异常(在except块中显示错误消息)并a保持不变,因此最后一行将打印“Hello”。
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))
回答by Terry Jan Reedy
To illustrate the comments, from 3.4.2 Idle Shell on Windows, python.org (PSF) installer
为了说明注释,来自 3.4.2 Idle Shell on Windows,python.org (PSF) 安装程序
>>> n = int(input('Guess1: '))
Guess1: 2
>>> n2 = float(input('Guess2: '))
Guess2: 3.1
>>> n, n2
(2, 3.1)
What system are you using and how did you install Python?
你使用的是什么系统,你是如何安装 Python 的?
回答by Martin Tournoij
However, I noticed something odd. The code works if I run it just by using the traditional run (i.e., the green button) that runs the entire code, rather than trying to execute individuals lines of code by pressing F2. Does anyone know why this may be the case?
然而,我注意到了一些奇怪的事情。如果我只使用运行整个代码的传统运行(即绿色按钮)来运行它,而不是尝试通过按 F2 来执行单个代码行,那么代码就可以工作。有谁知道为什么会这样?
This seems to be a problem in Eclipse, from the PyDev FAQ:
这似乎是 Eclipse 中的一个问题,来自PyDev FAQ:
Why raw_input() / input() does not work correctly in PyDev?
The eclipse console is not an exact copy of a shell... one of the changes is that when you press in a shell, it may give you a \r, \n or \r\n as an end-line char, depending on your platform. Python does not expect this -- from the docs it says that it will remove the last \n (checked in version 2.4), but, in some platforms that will leave a \r there. This means that the raw_input() should usually be used as raw_input().replace('\r', ''), and input() should be changed for: eval(raw_input().replace('\r', '')).
为什么 raw_input() / input() 在 PyDev 中不能正常工作?
eclipse 控制台不是 shell 的精确副本......其中一个变化是,当您按下 shell 时,它可能会给您一个 \r、\n 或 \r\n 作为结束行字符,具体取决于在您的平台上。Python 不希望这样——从文档中它说它将删除最后一个 \n (在 2.4 版中检查),但是,在某些平台上会留下一个 \r 。这意味着 raw_input() 通常应用作 raw_input().replace('\r', ''),而 input() 应更改为: eval(raw_input().replace('\r', ' '))。
Also see: PyDev 3.7.1 in Eclipse 4 — input() prepends prompt string to input variable?, Unable to provide user input in PyDev console on Eclipse with Jython.
另请参阅: Eclipse 4 中的 PyDev 3.7.1 — input() 将提示字符串添加到输入变量?, 无法在带有 Jython 的 Eclipse 上的 PyDev 控制台中提供用户输入。

