python input() EOF error and return value of type int?

岁酱吖の 提交于 2021-01-28 04:08:34

问题


I have 2 questions on these 2 short codes.

1.

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

output

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

Why does it give me this error?

2.

name = input('what: ')

print(type(name))

output:

what: 23
<type 'int'>

Process finished with exit code 0

Why is name type int? I thought all input are converted to string.

Please please please help me understand this. It's killing me.


回答1:


Use raw_input instead of input in python 2. That's how you get strings. Otherwise python will evaluate what you give it, which is giving you your unexpected behaviour.




回答2:


This is the behavior in Python 2.7. Whatever you accept via input will be converted to type int and whatever you accept via raw_input, will be converted to type str.

For behavior in Python 3, check this: What's the difference between raw_input() and input() in python3.x?



来源:https://stackoverflow.com/questions/46292743/python-input-eof-error-and-return-value-of-type-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!