如何将输入读取为数字? [英] How can I read inputs as numbers?
问题描述
为什么在下面的代码中使用x
和y
字符串而不是整数?
Why are x
and y
strings instead of ints in the below code?
(注意:在Python 2.x中使用raw_input()
.在Python 3.x中使用input()
.raw_input()
在Python 3.x中被重命名为input()
)
(Note: in Python 2.x use raw_input()
. In Python 3.x use input()
. 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
推荐答案
TLDR
- Python 3不会评估通过
input
函数接收到的数据,但是Python 2的input
函数会进行评估(请阅读下一节以了解含义). - Python 2等同于Python 3
input
是raw_input
函数.
- Python 3 doesn't evaluate the data received with
input
function, but Python 2'sinput
function does (read the next section to understand the implication). - Python 2's equivalent of Python 3's
input
is theraw_input
function.
Python 2.x
有两个函数可以获取用户输入,称为 input
和 raw_input
.它们之间的区别在于,raw_input
不评估数据,而是以字符串形式按原样返回.但是,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
作为input
的结果返回并存储在data
变量中.您可以将input
视为由 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中使用input
时应小心.我在此答案中解释了为什么在使用它时应格外小心.
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的 input
和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不会评估和转换数据类型,因此您必须使用
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'
对于可以包含小数部分的值,类型应为float
而不是int
:
For values that can have a fractional component, the type would be float
rather than int
:
x = float(input("Enter a number:"))
除此之外,您的程序可以像这样更改一点
Apart from that, your program can be changed a little bit, like this
while True:
...
...
if input("Play again? ") == "no":
break
您可以使用break
和while True
摆脱play
变量.
You can get rid of the play
variable by using break
and while True
.
这篇关于如何将输入读取为数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!