如何将输入读为数字?
为什么 x
和 y
字符串而不是以下代码中的ints?
(注意:在Python 2.x中使用 RAW_INPUT()
。in Python 3.x使用 input()
。 raw_input()
被重命名为 input()
在Python 3.x中)
play = True
while play:
x = input("Enter a number: ")
y = input("Enter a number: ")
print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x % y)
if input("Play again? ") == "no":
play = False
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
自Python 3以来noreferrer“>
int
,这样,您可以接受类似的基础的数字,
第二个参数将其告诉它是数字的基础,然后在内部理解和转换。如果输入的数据是错误的,它将抛出
valueerror
。对于可以具有分数组件的值,类型为
float
而不是int
:Python 2和3
摘要
input> Input
函数不再执行此操作。输入
是raw_input
函数。python 2.x
有两个功能可以获取用户输入,称为
input
和raw_input
。它们之间的区别是,raw_input
没有以字符串形式评估数据并返回。但是,输入
将评估您输入的任何内容,并将返回评估结果。例如,评估数据
5 + 17
,结果为22
。当它评估表达式5 + 17
时,它检测到您正在添加两个数字,因此结果也将为相同的int
类型。因此,免费完成类型转换,22
是Input
的结果,并存储在data
变量中。您可以将输入
视为raw_input
由est
呼叫。注意:在Python 2.x中使用
输入
时,您应该小心。我解释了为什么在使用时应该小心,in 这个答案。但是,
raw_input
不作为字符串评估输入和返回。python 3.x
python 3.x's 和python 2.x的
raw_input
相似,raw_input
在Python 3.x中不可用。Since Python 3,
input
returns a string which you have to explicitly convert toint
, like thisYou can accept numbers of any base like this
The second parameter tells it the base of the number and then internally it understands and converts it. If the entered data is wrong it will throw a
ValueError
.For values that can have a fractional component, the type would be
float
rather thanint
:Differences between Python 2 and 3
Summary
input
function evaluated the received data, converting it to an integer implicitly (read the next section to understand the implication), but Python 3'sinput
function does not do that anymore.input
is theraw_input
function.Python 2.x
There were two functions to get user input, called
input
andraw_input
. The difference between them is,raw_input
doesn't evaluate the data and returns as it is, in string form. But,input
will evaluate whatever you entered and the result of evaluation will be returned. For example,The data
5 + 17
is evaluated and the result is22
. When it evaluates the expression5 + 17
, it detects that you are adding two numbers and so the result will also be of the sameint
type. So, the type conversion is done for free, and22
is returned as the result of theinput
and stored in thedata
variable. You can think ofinput
as theraw_input
composed with aneval
call.Note: You should be careful when you are using
input
in Python 2.x. I explained why one should be careful when using it, in this answer.But,
raw_input
doesn't evaluate the input and returns as it is, as a string.Python 3.x
Python 3.x's
input
and Python 2.x'sraw_input
are similar andraw_input
is not available in Python 3.x.在Python 3.x中,
RAW_INPUT
被重命名为Input
,然后删除了Python 2.x 2.x input 。这意味着,就像
raw_input
, 在Python 3.x中总是返回字符串对象。要解决问题,您需要通过将这些输入放入 int :
In Python 3.x,
raw_input
was renamed toinput
and the Python 2.xinput
was removed.This means that, just like
raw_input
,input
in Python 3.x always returns a string object.To fix the problem, you need to explicitly make those inputs into integers by putting them in
int
:对于单行中的多个整数,
map
更好。如果已经知道该数字(例如2个整数),则可以使用
For multiple integer in a single line,
map
might be better.If the number is already known, (like 2 integers), you can use
input()
(Python 3)和RAW_INPUT()
(Python 2) emwly 返回字符串。用int()
将结果明确转换为整数。input()
(Python 3) andraw_input()
(Python 2) always return strings. Convert the result to integer explicitly withint()
.多个问题需要在一行上输入多个整数。最好的方法是按行进入整个数字的整个字符串,然后将其分成整数。这是Python 3版本:
您还可以使用列表综合:
要将所有输入从字符串转换为int,我们进行以下操作:
列表元素应以直线打印。
Multiple questions require multiple integers to be entered on a single line. The best way is to enter the entire string of numbers line by line and split them into integers. Here is the Python 3 version:
You can also use list comprehensions:
To convert all input from string to int we do the following:
List elements should be printed in straight lines.
转换为整数:
类似于浮点数:
Convert to integers:
Similarly for floating point numbers:
for循环应运行“ n”次数。第二个“ n”是阵列的长度。
最后一个语句将整数映射到列表,并以空格分离的形式获取输入。
您也可以在循环的末端返回阵列。
the for loop shall run 'n' number of times . the second 'n' is the length of the array.
the last statement maps the integers to a list and takes input in space separated form .
you can also return the array at the end of for loop.
我遇到了一个问题的问题,即在
尽管
int(input())
对于一个整数就足够了,但我找不到直接输入两个整数的方法。我尝试了一下:现在,我将
num1
和num2
用作整数。I encountered a problem of taking integer input while solving a problem on CodeChef, where two integers - separated by space - should be read from one line.
While
int(input())
is sufficient for a single integer, I did not find a direct way to input two integers. I tried this:Now I use
num1
andnum2
as integers.在您的示例中,
int(input(...))
在任何情况下都可以做到这一点,python-future
'sincoreins.input
值得考虑,因为这确保您的代码可适用于Python 2和3 and 禁用input
的Python2的默认行为,试图对输入数据类型“聪明”(nelidens.input.input
基本上只是raw_input
/代码>)。While in your example,
int(input(...))
does the trick in any case,python-future
'sbuiltins.input
is worth consideration since that makes sure your code works for both Python 2 and 3 and disables Python2's default behaviour ofinput
trying to be "clever" about the input data type (builtins.input
basically just behaves likeraw_input
).