Feb-24-2022, 09:46 AM
You should split your program into 2 - 3 parts.
One trick to count conditions, which are
- A function, which asks the user to input a valid integer. If the user enters something different, he should see an error message and the question should be repeated until the user has entered a valid integer. Users are humans and they fail very often. This is a kind of input validation.
- A function which uses the previous function 10 times to ask the user for numbers and collect those numbers in a list. This can be done with a for-loop or a list comprehension. Copy&Paste 10 Lines is counterproductive.
- A function or code block on module level to output the results from the previous function.
One trick to count conditions, which are
True
, can be done with the sum function.some_values = [1, 3, 8, 9, 13, 42] # a generator expression in a function call even_values = sum(value % 2 == 0 for value in some_values) # the unrolled version as a for-loop: results = [] # <- the list contains after the for-loop True and False booleans. for value in some_values: # value % 2 == 0, if this is True, it's an even number results.append(value % 2 == 0) even_values2 = sum(results) # it translates to: # sum((False, False, True, False, False, True))This works because a
bool
is a subtype of int
.0 == False 1 == True
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!