问题
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