It’s possible to “break out” of the infinite loop with break as well:
while True:
n = int(input())
print('The input was:', n)
if n == -1:
break
print('End of the program!')
Input
Output
10
20
16
8
0
2
-1
The input was: 10
The input was: 20
The input was: 16
The input was: 8
The input was: 0
The input was: 2
The input was: -1
As soon as the inputted number is -1, the program stops the loop. We can break the loop at an appropriate time to stop the program from executing an unwanted thing for specific input.
Challenge
Given a sequence of integers, we are interested in how many times is the largest number in the sequence repeated.
The sequence is given as a stream of integers, and the sequence is terminated as soon as the inputted number is equal to 0. The rest of the input should be ignored.
The program should output a single integer - the number of times the maximum element in the sequence is repeated.
Input
Output
5
5
1
1
5
3
0
9
11
4
5
3
Explanation: The largest number is 5 and it appears 3 times before the sequence is terminated.