Beginner · While loops · 85 of 264
Guess the number
The first line is a secret number. The lines after it are guesses. Count the guesses until (and including) the first correct one.
Input: the secret, then guesses one per line (one of them is right). Output: the number of guesses it took.
Examples
Input
7 3 9 7
Output
3
Input
42 42
Output
1
Input
5 1 2 3 4 5
Output
5
One more test checks other inputs when you press Check.
Stuck? The idea behind while loops
A while loop repeats as long as its condition is true: use it when you don't know in advance how many times.
Something inside the loop must change, so the condition becomes false in the end.
n = int(input())
digits = 0
while n > 0:
n //= 10
digits += 1
print(digits)Watch for: If nothing in the loop changes the condition, it never ends and the program is stopped after a few seconds.