0

I'd like to separate the input by an blank line, repeat reading the input until 2 blank lines are received. Here is the expected input format:

A
B
C

A B 2
A C 3
C B 4

A B 1


I have tried

for line in sys.stdin:
    node = [line]
    if line == ' ':
        cost = [line.split()]
        if line == ' ':
            distance = [line.split()]

But it can not stop reading input with 2 blank lines. Any advice?

1
  • line == ' ' - is line equal to single space character. Are use sure that this is what you need? Commented May 7, 2021 at 6:33

1 Answer 1

1

This is a very simple loop that stores whatever input you give in lines

when last 2 lines are empty i.e lines[-1]==lines[-2]=="" then break; [just mak sure that you have taken at least 2 inputs from the user so check len(lines)>2]

lines = []
while True:
    inp = input()
    lines.append(inp.strip())
    if len(lines)>2 and lines[-1]==lines[-2]=="":
        break
print('\n'.join(lines))
A
B
C

A B 2
A C 3
C B 4

A B 1


If you don't want to store all the lines

lines = ['temp', 'temp']
while True:
    inp = input()
    lines[-2], lines[-1] = lines[-1], inp.strip() # this changes the second last element by last one and the last one is updated to the new line that is entered
    if lines[-1]==lines[-2]=="":
        break
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but can I store lines in separate list? I mean I'd like to have a list store everything until reach one blank line, then a second list store the following until reaching one blank line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.