将多行用户输入转换为单行

converting multilines user input into single line

我刚开始编程并创建了我的第一个代码,它是一个字计数器。 当我测试我的代码时,它适用于交互式 window 但不适用于终端,因为终端无法接受多行输入,例如段落。因此,当我将我的代码编译成 exe 时,它​​无法工作,除非我以某种方式将整个段落转换为一行。有谁知道这个问题的任何解决方法?谢谢。

你好,我看到有人要求代码以获得更好的帮助,所以在这里进行了编辑(p.s:虽然代码工作得很好,但它不能接受整个段落作为输入,因为有 'enter' 在段落中,这算作用户输入并依次 运行 下一个代码,并打破它们。):



#"full filter" show original word lenght, filter lenght, and filtered word lenght.
print('James first coding, Words Counter .2x')
sentence = str(input("""give me the word that you want to analyse: """)).replace(",","").replace('"',"").replace(".",'').replace("!",'').replace("?",'').replace(":",'').replace(";",'').replace("$",'')
words = sentence.split()
word_lim = int(input("minimum world lenght: "))
result = []

for word in words:
    if len(word) > word_lim:
        result.append(word)


print("original word lenght: ",len(words))
print("filter lenght: ",word_lim)
print("Result:  ")
print("Total words: ", len(result))
print("total alphabets:",sum([len(x) for x in result]))
input('Press ENTER to exit')





试试这个:

print("Enter your content, Ctrl-D and Enter to save it.") 
contents = [] 
while True: 
    try: 
        line = input() 
    except EOFError: 
        break 
    contents.append(line)
print(*contents, sep = " ") 

在多行中输入您的字符串。完成多行用户输入后,按 ctrl+d。它向您的系统发送 signalEOF

如果您是 windows 用户,请使用 ctrl+z 而不是 ctrl+d。还有 enter.