CODING — Esercizio 2
📋 Testo
Exercise 2: Interruption upon insertion of two identical consecutive numbers
Write a program in Python that prompts the user for a sequence of integers.
The program must continue to read input numbers and must stop immediately when the user enters the same exact number twice in a row.
At the end of execution, the program must print a closing message indicating what the repeated number is.
Execution example:
Enter a number: 12
Enter a number: 4
Enter a number: 5
Enter a number: 5
Reading interrupted: the number 5 has been entered twice consecutively!
Analisi: This exercise introduces the student to the concept of state management between iterations of a loop. The student must memorize the value entered in the previous iteration to be able to compare it with the current one.
### Tutor Tips:
- Suggest the student read a first number before starting the cycle, in order to have an initial comparison value ('previous').
- Show how to update the previous number variable at the end of each iteration: `previous = current`.
### Common errors:
1. Initialize the 'previous' number variable with a fixed value (e.g. 0), which could erroneously cause the loop to end if the user types 0 immediately.
2. Compare the variable with itself within the same loop without updating its state correctly.
3. Mishandling the first input.