6
>>> 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'

It seems that whatever I throw in input in hopes to break it, python just keeps one upping me. It like knows what I'm trying to achieve here and goes "nope"

Konulv
  • 137
  • 7
  • 4
    So, you are complaining that it works properly? – Klaus D. Dec 30 '18 at 14:25
  • 2
    amusing question. no, its like expecting a string to break something. a string that is not being evaluated to be specific. – Paritosh Singh Dec 30 '18 at 14:25
  • 1
    Why would it throw an error? It just registers what you type in. That's what it's for. – Niels Henkens Dec 30 '18 at 14:25
  • 2
    People need to cheer up. This is an interesting question. +1 – timgeb Dec 30 '18 at 14:27
  • its not that i'm complaining that it works properly, i'm just genually curiuous if its possible for it to break – Konulv Dec 30 '18 at 14:27
  • 4
    the only thing I have in mind is to do ctrl + C for throw KeyboardInterupt exception, But this is a normal behavior. – iElden Dec 30 '18 at 14:29
  • Probably worth pointing out that much of what your example is showing is the effect of `repr` on the returned string. Try using `print(input('input'))` in a loop and you will see that `input` always returns the same string that was typed in. – Daniel Pryden Dec 30 '18 at 14:36
  • my initial idea was that `input` wraps input in `' '`, so first thought that came to mind if i can circumvent that, but then it just started wrapping input in `" "` and later attempts with `\` – Konulv Dec 30 '18 at 14:38
  • 2
    It’s not wrapping anything. It returns a string. A string isn’t wrapped in quotes, a string just contains its contents, which is whatever you typed in. Only when you print a *string representation* will quotes come into play, and Python is perfectly capable of producing any valid string representation regardless of the string’s content. – deceze Dec 30 '18 at 14:41
  • 2
    @Konulv: `input` *never* wraps input in quotes or anything else. It just returns a string. `repr` (which the Python interactive prompt uses when printing things) attempts to construct a string which, when parsed by Python, would produce the same string it received as input. So what you're seeing here is the behavior of `repr`, nothing to do with `input` at all. – Daniel Pryden Dec 30 '18 at 14:42
  • so you are saying i should be looking into how to break `repr`? xD @DanielPryden – Konulv Dec 30 '18 at 14:53

6 Answers6

2

The EOFError mentioned by others can also be triggered by standard input being something that isn't an infinite stream (like standard input generally is), such as /dev/null:

$ python3 -c 'input("")' < /dev/null
Traceback (most recent call last):
  File "<string>", line 1, in <module>
EOFError: EOF when reading a line

In a similar vein, we could also write input that's impossible to decode as UTF-8 (here using xxd to decode hex into bytes that are then passed to 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
AKX
  • 152,115
  • 15
  • 115
  • 172
1

The easiest way I can think of is to press CTRL+D which will trigger an EOFError.
Similary, CTRL+C will raise a KeyboardInterrupt.

Demo:

>>> input('in: ')
in: Traceback (most recent call last): # <-- I pressed CTRL-D
  File "<stdin>", line 1, in <module>
EOFError

Maybe a more interesting way to get the EOFError is to use an empty file as the input to your program.

$ 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
timgeb
  • 76,762
  • 20
  • 123
  • 145
1

Not exactly an answer to the question, but you can throw a UnicodeDecodeError by changing the default encoding in python and then trying to input a unicode character (such as an emoji) which follows a different encoding scheme.

$ 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)
cs95
  • 379,657
  • 97
  • 704
  • 746
1

Based on the comments on the question here, it appears that the actual confusion stems from how the Python interactive prompt displays the return value of input. The interactive session always displays values using repr, which is designed to try to print a string which, when parsed, would produce the original value. For strings this can lead to some confusion, since the thing that is printed in the interactive session is not the actual string, but a representation of the string.

To see the difference, you can try playing around with this program, which will likely be instructive:

#!/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()

Here are some examples of what it prints:

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'
Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
0

Well Well Well,

Try this little bugger. enter image description here

Apparently you can raise EOF errors with the right symbol or with ctrl + D. Docs

Paritosh Singh
  • 6,034
  • 2
  • 14
  • 33
  • purposefully inserts a pic so i cant get that character xD. Is there a good reason for that tho? like its not part of whats in ASCII table that python uses or something else? – Konulv Dec 30 '18 at 14:42
  • Well, the issue is that i actually have that character pasted, but im guessing it does not carry over. You see that box next to my sentence `try this little bugger`? It's supposed to be there, but its apparently not or it gets lost in conversion when i paste it here :P @Konulv – Paritosh Singh Dec 30 '18 at 14:44
  • 1
    @Konulv [relevant](https://stackoverflow.com/questions/12389518/representing-eof-in-c-code) – timgeb Dec 30 '18 at 14:48
  • Yay for windows! thanks for the link @timgeb much appreciated – Paritosh Singh Dec 30 '18 at 14:50
0

In that case, change the environment from python 3.0 to python 2.7

#!/usr/bin/env python
text=input("Enter somthing that throws error!!!")

Then whatever you put it will throw an error, For instance, consider "dude" as a string then it throws the following error

File "<string>", line 1, in <module>
NameError: name 'dude' is not defined