>>> 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'
看来,无论我投掷input
来打破它的意图,python都会不断地提升我。就像知道我在这里想要实现的目标并“不行”
答案 0 :(得分:2)
其他人提到的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
同样,我们也可以编写input that's impossible to decode as 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
答案 1 :(得分:1)
我想到的最简单的方法是按 CTRL + D ,这将触发EOFError
。
类似地, CTRL + C 将引发KeyboardInterrupt
。
演示:
>>> input('in: ')
in: Traceback (most recent call last): # <-- I pressed CTRL-D
File "<stdin>", line 1, in <module>
EOFError
也许获取EOFError
的一种更有趣的方法是使用一个空文件作为程序的输入。
$ cat input.py
got = input('in: ')
print(got)
$ echo some text > not_empty.txt
$ python3 input.py < not_empty.txt
in: some text
$ touch empty.txt
$ python3 input.py < empty.txt
in: Traceback (most recent call last):
File "input.py", line 1, in <module>
got = input('in: ')
EOFError: EOF when reading a line
答案 2 :(得分:1)
不是完全正确的答案,但是您可以通过更改python中的默认编码并尝试输入一个Unicode字符(例如表情符号)来抛出UnicodeDecodeError
遵循不同的编码方案。
$ export PYTHONIOENCODING=ascii
$ python3.6
Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> input('>>> ')
>>>
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf0 in position 0: ordinal not in range(128)
答案 3 :(得分:1)
基于对此处问题的评论,似乎真正的困惑源于Python交互式提示如何显示input
的返回值。交互式会话始终使用repr
显示值,该值设计为尝试打印一个字符串,该字符串在解析后会产生原始值。对于字符串,这可能会引起一些混乱,因为在交互式会话中打印的内容不是实际的字符串,而是字符串的表示形式。
要了解两者之间的区别,您可以尝试使用此程序,这可能会很有帮助:
#!/usr/bin/env python3
import unicodedata
def main():
while True:
s = input('Enter a string: ')
if not s:
break
print('Got string with length {}'.format(len(s)))
for i, c in enumerate(s):
print('Character {}: {}'.format(i, unicodedata.name(c)))
print('repr() produces: {}'.format(repr(s)))
print('String literally contains: {}'.format(s))
print()
if __name__ == '__main__':
main()
以下是打印内容的一些示例:
Enter a string: a
Got string with length 1
Character 0: LATIN SMALL LETTER A
repr() produces: 'a'
String literally contains: a
Enter a string: 'a
Got string with length 2
Character 0: APOSTROPHE
Character 1: LATIN SMALL LETTER A
repr() produces: "'a"
String literally contains: 'a
Enter a string: "a'
Got string with length 3
Character 0: QUOTATION MARK
Character 1: LATIN SMALL LETTER A
Character 2: APOSTROPHE
repr() produces: '"a\''
String literally contains: "a'
答案 4 :(得分:0)
答案 5 :(得分:0)
在这种情况下,将环境从python 3.0更改为python 2.7
#!/usr/bin/env python
text=input("Enter somthing that throws error!!!")
那么无论您输入什么内容,都将引发错误,例如,将“ dude”视为字符串,然后将引发以下错误
File "<string>", line 1, in <module>
NameError: name 'dude' is not defined