CODING — Esercizio Ram_1
📋 Testo
Exercise: Interruption on an even number
Write a program that continues to request and read integers entered by the user as input.
The program must end its execution as soon as an even number is entered.
The parity check must be inserted directly as a condition test of the
whileloop (without usingwhile Trueandbreak).At the time of interruption, the program must print a message indicating the conclusion of the loop and showing the last (even) number entered.
Execution example:
Enter a number: 5
Enter a number: 11
Enter a number: 3
Enter a number: 6
Cycle finished! You entered the even number: 6
Analisi: ### Educational Objective
This exercise teaches you to control the flow of a pre-conditioned (while) loop by inserting the logical parity test directly into the loop permanence condition, avoiding the use of infinite loops and break statements.
### Tips for guiding the student:
1. Explain that to use the variable in the `while` test, it must be initialized *before* the start of the loop (e.g. by reading a first input outside).
2. Show how the condition in the `while` represents the **stay** condition in the loop (the loop continues *until the number is odd*, i.e. `number % 2 != 0`), which is the logical opposite of the stopping condition.
3. Explain that once inside the loop, it is essential to request new input to avoid an infinite loop.
### Evaluation Criteria and Common Mistakes:
- **Initialization Error**: Forgetting to read the first input before the `while` loop, making the variable undefined in the test.
- **Inverted Conditional Logic**: Write `while number % 2 == 0:`, which would cause the loop to end immediately if the first number is odd, or it would never start.
- **Failure to Update**: Do not insert the input request inside the loop, causing an infinite loop on the first number entered.