CODING — Esercizio 7
📋 Testo
Exercise 7: Sum of the divisible by 3 greater than the product of the even numbers
Write a program in Python that receives a sequence of integers as input.
The program must keep track of two distinct quantities:
- The sum of only the numbers entered that are divisible by 3.
- The product of all the even numbers entered.
The acquisition of numbers must stop as soon as the sum of the numbers divisible by 3 becomes strictly greater than the product of the even numbers.
Note on product initialization: The product of even numbers must be initialized to 1 to allow multiplication. If no even number has been entered yet, consider the product equal to 1.
When finished, print the sum of the divisible by 3, the product of the even numbers and the total number of elements inserted.
Execution example:
Enter a number: 9 (divisible by 3. Sum div3 = 9. Even product = 1)
Enter a number: 4 (even. Sum div3 = 9. Product even = 4)
Enter a number: 6 (even and div3. Sum div3 = 9 + 6 = 15. Even product = 4 * 6 = 24)
Enter a number: 12 (even and div3. Sum div3 = 15 + 12 = 27. Even product = 24 * 12 = 288)
Enter a number: 271 (does not affect. Sum div3 = 27. Even product = 288)
Enter a number: 300 (divisible by 3. Sum div3 = 27 + 300 = 327. Even product = 288)
Reading interrupted! The sum of the divisible by 3 (327) exceeded the product of the even numbers (288).
Analisi: This is an advanced exercise on conditional loops that requires the management of two independent accumulators with different update logics (sum for divisible by 3, multiplication for even).
### Tutor Tips:
- Explain that a number can satisfy both conditions at the same time (e.g. the number 6 is both even and divisible by 3), in which case it must be added to the sum AND multiplied into the product.
- Emphasize that the product variable must be initialized to 1, since initializing it to 0 would always result in 0 for any multiplication.
- The stopping condition is `sum_three > even_product`.
### Common errors:
1. Initialize the product to 0.
2. Not properly handling numbers that meet both criteria (e.g. using an `elif` instead of two separate `if`s).
3. Incorrectly calculate the shutdown before starting the loop or before updating the current values.