4

python

input(write some text: )

what i want it to output is:

write some text: it was a good day
today as i went to a park

then press enter button twice to go on to the other lines of code

  • 2
    Does this answer your question? [How to get multiline input from user](https://stackoverflow.com/questions/30239092/how-to-get-multiline-input-from-user) – AverageHomosapien Feb 07 '21 at 10:01

2 Answers2

3
print("enter 'quit' at end of your text")
print("type your text here")
# declare variable to strore string
text = ""
stop_word = "quit"
while True:
    line = input()
    if line.strip() == stop_word:
        break
    # \n is new line, %s is string formatting
    # str.strip() will remove any whitespace that is at start or end
    text += "%s\n" % line.strip()
print(text)

if you want use 3 blank lines as stop word:

print("Please enter 3 blank lines to terminate")
print("type your text here")
# declare variable to strore string
text = ""
stop_word = "quit"
counter = 0
while True:
    line = input()
    if line.strip() == '':
        counter += 1
        if counter == 3:
            break
    # \n is new line, %s is string formatting
    # str.strip() will remove any whitespace that is at start or end
    text += "%s\n" % line.strip()
print(text)

for the result, if you want to get rid of blank lines:

print("Please enter 3 blank lines to terminate")
print("type your text here")
# declare variable to strore string
text = ""
stop_word = "quit"
counter = 0
while True:
    line = input()
    if line.strip() == '':
        counter += 1
        if counter == 3:
            # break terminates while loop
            break
        # continue go to start of while loop
        continue
    # \n is new line, %s is string formatting
    # str.strip() will remove any whitespace that is at start or end
    text += "%s\n" % line.strip()
print(text)
Weilory
  • 2,621
  • 19
  • 35
  • how can i change it to do 3 lines and then go onto the next code? –  Feb 07 '21 at 10:14
  • i more question, in line variable when the user does the input it will include the 3 lines that don't induce any thing. anyway to change that without changing the t enters in the input field? –  Feb 07 '21 at 10:22
3

You can use the sys.stdin.readlines to read multiple lines, and break by using ctrl + z (on windows) / ctrl + d.

import sys

msg = sys.stdin.readlines()
AverageHomosapien
  • 609
  • 1
  • 7
  • 15