Series34. Integers K, N and K sets of integers with N elements in each set are given. For each set, perform the following action: if the set contains the number 2, then output the sum of its elements; If there are no 2-s in the set, output 0.

Solution in Python 3:

import random

K = random.randrange(1,7)
N = random.randrange(1,7)

print("K = ",K)
print("N = ",N)

for i in range(0,K):
s = 0
check = False
for j in range(0,N):
x = random.randrange(0,4)
print(x,end="; ")
s += x
if x == 2:
check = True
print()
print("Sum = ",end="")
if check:
print(s)
else:
print(0)
print()