Timaayy
Timaayy

Reputation: 848

Python unexpectedly stopping while loop after getting input multiple times and printing the input

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).

Upvotes: 0

Views: 82

Answers (1)

Alex Hall
Alex Hall

Reputation: 36013

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.

Upvotes: 1

Related Questions