from itertools import product variants = list(product('123456', repeat=3)) counter = 0 for var in variants: total = sum(int(i) for i in var) if total == 6: counter += 1 print(counter)