0

I want to support multiple line input for raw_input, ctrl+d in the following code works only when I am on an empty (new) line,

Current behaviour:

  • when user types xyz, then press enter again to get a new line, then ctrl+d.
  • when user types xyz, then press ctrl+d 4 times, the first three times it has no effect, the 4th time makes it exit the function.

expected behaviour:

when user types xyz, then ctrl+d to exit the function(without needing to be on an empty line).


def multi_line_input(prompt=''):
    contents = []
    print('ctrl+d to exit')
    i = 0
    while True:
        try:
            if i:
                line = raw_input()
            else:
                line = raw_input(prompt)
            i += 1
        except EOFError:
            break
        contents.append(line)

    return '\n'.join(contents).strip()


x = multi_line_input('please enter text >> ')
print ('user entered:', x)
Shuman
  • 3,914
  • 8
  • 42
  • 65
  • *"Currently the behavior is, type xyz, then press enter again to get a new line, then ctrl+d."* That's not the behaviour of your code, it's the behaviour of the user! What happens if you *do* type `xyz` then Ctrl-D? – kaya3 Nov 12 '19 at 21:05
  • https://stackoverflow.com/questions/7369170/why-do-i-have-to-type-ctrl-d-twice this answers your question? – nonamer92 Nov 12 '19 at 21:15
  • @ranifisch No it did not, for me I have to press it 4 time instead of 2 times. – Shuman Nov 12 '19 at 21:43
  • This is likely terminal-dependent behavior, as it is your *terminal* that intercepts Control-D and interprets it as a request to *close* standard input, rather than producing a character that *appears* in standard input. – chepner Nov 12 '19 at 21:56

0 Answers0