Home The Project Blog Plans Mission
Login Register
Home
CODING Es. 4 Pag. 1

CODING — Esercizio 4

🎓
You are exploring a public demo
Sign up to access all exercises and save your progress.
Sign up for free →

📋 Testo

Exercise 4: Calculating the average with interruption


Write a program in Python that reads a series of integers entered by the user.


The program must continuously recalculate the average of the numbers entered up to that moment and must interrupt reading as soon as the average of the numbers entered is greater than 10.


Note: the break must occur after at least one insertion to avoid divisions by zero at the beginning.


At the end, the program must show the final average and the number of elements inserted.



Execution example:


Enter a number: 5
Enter a number: 8
Enter a number: 20
Reading interrupted! Current average: 11.0 (greater than 10). Number of entries: 3.

Analisi: The exercise introduces an additional level of complexity by combining an accumulator (the sum) and a counter to calculate a derived statistic (the average) at each step of the loop.

### Tutor Tips:
- Highlight the risk of division by zero at the beginning if the calculation is attempted before the user enters at least one number.
- Guide the student to update the sum and count at each iteration, calculating the average as `sum / count`.
- Explain how to structure the loop condition so that the check occurs after entering the first value.

### Common errors:
1. Division by zero at program startup.
2. Incorrect calculation of the average (e.g. average calculated by dividing only by a fixed value).
3. Incorrect logic conditions causing premature termination or infinite loops.

Your Solution Reflection Code
▶ Write your solution-reflection and send it for a guided review.
Socratic AI Tutor
TUTOR AI

Contact us for a free trial