我对这 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/