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)