>>> input("input")
input>? a
'a'
>>> input("input")
input>? 'a
"'a"
>>> input("input")
input>? '"a
'\'"a'
>>> input("input")
input>? \'"a
'\\\'"a'
>>> input("input")
input>? "'a
'"\'a'
>>> input("input")
input>? "''a
'"\'\'a'
Run Code Online (Sandbox Code Playgroud)
似乎无论我投入什么都input
希望打破它,python只是让我加油.它知道我想要在这里实现什么并且去"不"
其他人提到的 EOFError 也可以由不是无限流的标准输入触发(就像标准输入通常一样),例如/dev/null
:
$ python3 -c 'input("")' < /dev/null
Traceback (most recent call last):
File "<string>", line 1, in <module>
EOFError: EOF when reading a line
Run Code Online (Sandbox Code Playgroud)
同样,我们也可以编写无法解码为 UTF-8 的输入(此处用于xxd
将十六进制解码为字节,然后传递给 Python)。
$ echo 'fe fe ff ff' | xxd -r -ps | python3 -c 'print(input())'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfe in position 0: invalid start byte
Run Code Online (Sandbox Code Playgroud)