只返回Str类型Jupyter Noteb的Input语句

2024-03-29 00:10:49 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here

虽然我输入了一个int,但我很难理解为什么返回str。有人能解释一下吗?当您在input中输入值时,它是否只将值捕获为字符串?你知道吗


Tags: 字符串inputintstr
1条回答
网友
1楼 · 发布于 2024-03-29 00:10:49

在Python 3.x中,^{}按原样返回输入的值(str),而不是对其求值,因此应该执行int(input('Please input number:'))以获取值作为int。你知道吗

但是,在Python 2.x中,^{}将返回原始的str值,而^{}将计算输入的值,正如您在文档中看到的:

input([prompt])

Equivalent to eval(raw_input(prompt)).

...

Consider using the raw_input() function for general input from users.

如果确实需要的话,可以用^{}eval(input('Please input number:'))在Python3.x中模拟这种行为,但是首先看一下Security of Python's eval() on untrusted strings?

相关问题 更多 >