Python 提示输入,直到给出2个空行
我需要提示用户输入,直到一行中给出两个空行,请注意,为清晰起见,输入读数中可能有空行,我需要在中断前背靠背给出两个空行 到目前为止,我已经想到了这个:Python 提示输入,直到给出2个空行,python,python-2.7,input,Python,Python 2.7,Input,我需要提示用户输入,直到一行中给出两个空行,请注意,为清晰起见,输入读数中可能有空行,我需要在中断前背靠背给出两个空行 到目前为止,我已经想到了这个: def gather_intel(): done = False while done is False: data = raw_input("Copy and paste the work log: ") if data is None: done = True 不管怎样
def gather_intel():
done = False
while done is False:
data = raw_input("Copy and paste the work log: ")
if data is None:
done = True
不管怎样,只要给出一个空行,就会结束,我还尝试添加另一个,而循环:
def gather_intel():
done = False
while done is False:
data = raw_input("Copy and paste the work log: ")
while data != "" + "\n" + "":
data = raw_input("Copy and paste the work log: ")
if data == "" + "\n" + "":
done = True
然而,这是一个无限循环,永远不会结束。我如何提示用户输入,直到有两个空白行背靠背地提供给输入?供未来的我或其他人使用。输入
直到两个连续换行符:
number_of_empty_responses = 0
while True:
data = raw_input("Copy and paste the work log: ")
if data == "":
number_of_empty_responses += 1
if number_of_empty_responses == 2:
break
else:
number_of_empty_responses = 0
pass # Received data, perform work.
def handy_input(prompt='> '):
'An `input` with 2 newline characters ending.'
all_input_strings = ''
# prompt the user for input
given_input = input(prompt)
all_input_strings += given_input
# and handle the two newline ending
while given_input:
# if previous input is not empty prompt again
given_input = input('')
all_input_strings += '\n' + given_input
return all_input_strings
问题的答案有两行空行:
def empty_lines_input(prompt='> ', n_lines=2):
'An `input` with `n_lines` empty line ending.'
all_input_strings = ''
# prompt the user for input
given_input = input(prompt)
all_input_strings += given_input
# and handle the two newline ending
while n_lines>0:
# if previous input is not empty prompt again
given_input = input('')
all_input_strings += '\n' + given_input
# check if it was an empty line
if not given_input:
n_lines -= 1
else:
n_lines = 2
return all_input_strings
一行两个空行,很抱歉没有指定,如中所示,用户在两个连续的提示中点击“回车”键?是的,先生,正是这样。当正好给出两个空行时,上述代码将中断,如果为了清楚起见,读取的数据中有空行,那么您需要在问题中澄清,因为它对于您正试图实现的目标是不明确的。