First time poster here (and pretty much completely new to python) with an undoubtedly easy question. I'll cut to the chase, here's my current code:
def even(x):
if x % 2 == 0:
even = True
else:
even = False
if even is True:
print("Even")
if even is False:
print("Odd")
N=[1,3,2,4]
for x in N:
even(x)
There were 1 Odd numbers
There were 1 Odd numbers
There were 1 Even numbers
There were 1 Even numbers
There were 2 Odd numbers
There were 2 Even numbers
You can use sum()
and map()
:
def even(x):
return (x % 2 == 0)
N = [1,3,2,4,6,8]
n_even = sum(map(even, N))
print(n_even)
# 4
Now even
returns True
(1) if the number is even and False
(0) otherwise. Now simply sum it up and you have the times an even number occurred.
Additionally, you might want to define n_odd
as
n_odd = len(N) - n_even