2024-03-29 00:10:49 发布
网友
虽然我输入了一个int,但我很难理解为什么返回str。有人能解释一下吗?当您在input中输入值时,它是否只将值捕获为字符串?你知道吗
在Python 3.x中,^{}按原样返回输入的值(str),而不是对其求值,因此应该执行int(input('Please input number:'))以获取值作为int。你知道吗
3.x
str
int(input('Please input number:'))
int
但是,在Python 2.x中,^{}将返回原始的str值,而^{}将计算输入的值,正如您在文档中看到的:
2.x
input([prompt])Equivalent to eval(raw_input(prompt))....Consider using the raw_input() function for general input from users.
input([prompt])
Equivalent to eval(raw_input(prompt)).
eval(raw_input(prompt))
...
Consider using the raw_input() function for general input from users.
raw_input()
如果确实需要的话,可以用^{}:eval(input('Please input number:'))在Python3.x中模拟这种行为,但是首先看一下Security of Python's eval() on untrusted strings?
eval(input('Please input number:'))
在Python} 按原样返回输入的值(
3.x
中,^{str
),而不是对其求值,因此应该执行int(input('Please input number:'))
以获取值作为int
。你知道吗但是,在Python} 将返回原始的} 将计算输入的值,正如您在文档中看到的:
2.x
中,^{str
值,而^{如果确实需要的话,可以用^{} :
eval(input('Please input number:'))
在Python3.x
中模拟这种行为,但是首先看一下Security of Python's eval() on untrusted strings?相关问题 更多 >
编程相关推荐