Search code examples
pythonmultiline

converting multilines user input into single line


I just started programming and created my first code which is a word counter. When I tested my code, it works on interactive window but not on the terminal because the terminal cannot accept multi-line input e.g. paragraphs. So, when I compile my code into exe, it cannot work unless I somehow convert the entire paragraph into a single line. Does anyone know any workaround to this problem? Thank you.

Hello, an edit here since I saw someone asked for the code for better help (p.s: the code worked perfectly though, it just can't accept a whole paragraph as input because there are 'enter' in paragraph, which counted as user enter and in turns, running the next code, and breaking them.):



#"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')






Solution

  • Try this:

    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 = " ") 
    

    Enter your string in multiple lines. Once you complete giving the user input in multiple lines, press ctrl+d. It sends a signalEOF to your system.

    If you are a windows user, use ctrl+z instead of ctrl+d. And enter.