0

Okay so I was trying out the Google Kick Start problems for the first time when I came across this problem. For some reason when I enter multiple lines of inputs at the same time and then print in between processing each input it makes the loop run forever until I enter in a new input. Is there a simple explanation for this scenario?

x = int(input())
print('\nx =', x)
for i in range(x):
    newInput = input()
    print(newInput)

If I put the input as:

3
1
2
3

Then the output is:

x = 3
1
2

and then I have to press 'enter' again to see

3

Why does the program require the 2nd input to get the remaining 3 to print? Curiously when I use the debugger mode with breakpoints on my IDE this problem disappears (no longer need to press 'enter' again).

1 Answer 1

1

Python waits for a newline when you call input(). If you copy paste the input data without a newline at the end it doesn't know that the last line is complete, but the lines before that end in newlines so it does them. Make sure that what you copy paste contains a newline at the end.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried both print(newInput + '\n') and print(newInput, '\n') but neither worked.
The print has nothing to do with this, it's how you feed input to the program. For example if I just copy your sample input 3 1 2 3 from the question it doesn't end in a newline, it's effectively "3\n1\n2\n3". I need to paste it into a file, add a newline at the end, then copy the whole thing.
I knew it had to be something incredibly simple that I was missing. Adding another line after the final 3 in my input is worked. Thanks

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.