如何将输入读作整数? [英] How can I read inputs as integers?
问题描述
为什么这段代码不输入整数?网上的所有内容都说使用 raw_input()
,但我读了Stack Overflow(在一个不处理整数输入的线程上), raw_input ()
在Python 3.x中被重命名为 input()
。
Why does this code not input integers? Everything on the web says to use raw_input()
, but I read on Stack Overflow (on a thread that did not deal with integer input) that raw_input()
was renamed to input()
in 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
推荐答案
Python 2.x
有两个函数可以获取用户输入,称为 输入
和 的raw_input
。它们之间的区别在于, raw_input
不会以字符串形式评估数据并按原样返回。但是,输入
将评估您输入的内容,并将返回评估结果。例如,
There were two functions to get user input, called input
and raw_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,
>>> import sys
>>> sys.version
'2.7.6 (default, Mar 22 2014, 22:59:56) \n[GCC 4.8.2]'
>>> data = input("Enter a number: ")
Enter a number: 5 + 17
>>> data, type(data)
(22, <type 'int'>)
评估数据 5 + 17
,结果为 22
。当它计算表达式 5 + 17
时,它会检测到你正在添加两个数字,因此结果也将是相同的 int
类型。因此,类型转换是免费的, 22
作为输入
的结果返回并存储在<$ c中$ c>数据变量。您可以将输入
视为由 raw_input 。 org / 2 / library / functions.html#evalrel =noreferrer> eval
来电。
The data 5 + 17
is evaluated and the result is 22
. When it evaluates the expression 5 + 17
, it detects that you are adding two numbers and so the result will also be of the same int
type. So, the type conversion is done for free and 22
is returned as the result of input
and stored in data
variable. You can think of input
as the raw_input
composed with an eval
call.
>>> data = eval(raw_input("Enter a number: "))
Enter a number: 5 + 17
>>> data, type(data)
(22, <type 'int'>)
注意:在Python 2.x中使用输入
时应该小心。我在这个答案中解释了为什么要使用它时要小心。
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.
但是, raw_input
不会评估输入并按字符串形式返回。
But, raw_input
doesn't evaluate the input and returns as it is, as a string.
>>> import sys
>>> sys.version
'2.7.6 (default, Mar 22 2014, 22:59:56) \n[GCC 4.8.2]'
>>> data = raw_input("Enter a number: ")
Enter a number: 5 + 17
>>> data, type(data)
('5 + 17', <type 'str'>)
Python 3.x
Python 3.x的 输入
和Python 2.x的 raw_input
类似,并且 raw_input
在Python 3.x中不可用。
Python 3.x's input
and Python 2.x's raw_input
are similar and raw_input
is not available in Python 3.x.
>>> import sys
>>> sys.version
'3.4.0 (default, Apr 11 2014, 13:05:11) \n[GCC 4.8.2]'
>>> data = input("Enter a number: ")
Enter a number: 5 + 17
>>> data, type(data)
('5 + 17', <class 'str'>)
解决方案
回答你的问题,因为Python 3.x没有要评估和转换数据类型,你必须使用 int s /functions.html#intrel =noreferrer> int
,就像这样
To answer your question, since Python 3.x doesn't evaluate and convert the data type, you have to explicitly convert to int
s, with int
, like this
x = int(input("Enter a number: "))
y = int(input("Enter a number: "))
您可以接受任何基数,并使用 int
函数将它们直接转换为基数10 ,像这样
You can accept numbers of any base and convert them directly to base-10 with the int
function, like this
>>> data = int(input("Enter a number: "), 8)
Enter a number: 777
>>> data
511
>>> data = int(input("Enter a number: "), 16)
Enter a number: FFFF
>>> data
65535
>>> data = int(input("Enter a number: "), 2)
Enter a number: 10101010101
>>> data
1365
第二个参数说明输入数字的基数是什么,然后是内部数据它理解并转化它。如果输入的数据错误,则会抛出 ValueError
。
The second parameter tells what is the base of the numbers entered and then internally it understands and converts it. If the entered data is wrong it will throw a ValueError
.
>>> data = int(input("Enter a number: "), 2)
Enter a number: 1234
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: invalid literal for int() with base 2: '1234'
除此之外,你的程序可以稍微更改一下,比如这个
Apart from that, your program can be changed a little bit, like this
while True:
...
...
if input("Play again? ") == "no":
break
您可以使用 break来摆脱
和 play
变量,而True
。
PS :Python不期望;
在行尾: )
PS: Python doesn't expect ;
at the end of the line :)
这篇关于如何将输入读作整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!