如何通过按回车来引入由换行符\ n完成的输入?

输入必须以换行符\ n结尾,并按回车键。

像这样:

n= input('') > must be-> 1,1,1 \n must press enter
                         1,0 \n must press enter
                         1,0 \n must press enter

然后是这样的:['1,1','1,0',0']

然后我要像这样用','分割字符串”:

a = ['1','0','1','1'],b =['1','0'],c...

我不了解的事情是如何通过按回车键'\ n'来分割输入,然后运行程序。例如,如果有一个number_of_times =您必须输入多少次,我会花一会儿时间,但是在此示例中,我现在不知道如何输入。

bierlaopo 回答:如何通过按回车来引入由换行符\ n完成的输入?

您可以这样做。如果您想使用number_of_times次尝试

number_of_times = int(input())
result = []
for i in range(number_of_times)
    line = input()
    result.append(line)

print(result)

否则您必须通过按CTRL + D

将EOF放在最后
import sys
result= []
for line in sys.stdin:
    result.append(line)
print(result)


,

只需使用这个简单的逻辑,这将为您提供预期的输出

lines = []
while True:
    line = input()
    if line:
        lines.append(line)
    else:
        break

list_input = '\n'.join(lines)
list_input = list_input.replace('\n',',')
final_list = list_input.split(',')
print(repr(final_list))
本文链接:https://www.f2er.com/2986505.html

大家都在问