python input() EOF错误并返回int类型的值?

标签 python python-2.x

我对这 2 个短代码有 2 个问题。

1.

name = input('what: ')
print(name)

输出

what: 641u
Traceback (most recent call last):
  File "/Users/vuthynun/PycharmProjects/untitled1/h/__init__.py", line 1, in <module>
    name = input('what: ')
  File "<string>", line 1
    641u
       ^
SyntaxError: unexpected EOF while parsing

Process finished with exit code 1

为什么会出现这个错误?

2.

name = input('what: ')

print(type(name))

输出:

what: 23
<type 'int'>

Process finished with exit code 0

为什么name类型是int?我以为所有输入都转换为字符串。

请帮我理解这一点。快要死我了。

最佳答案

在 python 2 中使用 raw_input 代替 input。 这就是你获得字符串的方式。否则 python 会评估你给它的东西,这会给你带来意想不到的行为。

关于python input() EOF错误并返回int类型的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46292743/

相关文章:

python - 如何在python中完成相对导入

python - __new__ 在对象创建中没有被调用

python - 如何强制 PyYAML 将字符串加载为 unicode 对象?

python - pickle:它如何 pickle 一个函数?

python - Pandas groupby : efficient conditional aggregation?

python - 出现以下错误消息 : error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'cv::contourArea'

python - Python 中的装箱时间列

Python 加密模块

python - Decimal 类可以处理的最大数字是多少?

python - TypeError 异常未被捕获