from random import * N = 4 a = range(1, 10) shuffle(a) a = ''.join(map(str,a[:N])) x = '' while x!=a: hit, blow = 0, 0 x = raw_input('Input %d digit number: ' % N) for i, e in enumerate(a): if x[i]==e: hit += 1 for e in a: if e in x: blow += 1 blow -= hit print '%d hit, %d blow' % (hit, blow)