The length of the sequence. Given a sequence of non-negative integers where each number is written on a separate line, determine the length of the sequence. The sequence ends when an integer equal to 0 is encountered. Print the length of the sequence, excluding the integer 0. Numbers following the number 0 should be omitted.
|
count = 0 while True: num = int(input()) if num == 0: break count += 1 print(count) |
Explanation of the Code
- Initialize Counter:
- Initialize a variable
count
to 0 to keep track of the number of integers in the sequence.
- Reading Input and Counting:
while True:
num = int(input())
if num == 0:
break
count += 1
- Use a
while True
loop to continuously read input.
- Convert the input to an integer using
int()
.
- If the integer is 0, break the loop.
- If the integer is not 0, increment the
count
by 1.
- Printing the Length of the Sequence:
- Print the value of
count
after exiting the loop.