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)
#初心者用
# coding:utf-8 import random print "数当てゲーム Hit & Blow" a = [] b = [] r = random.randint(1, 9) n = int(raw_input("遊びたい桁を入力してください(1桁以上): ")) s = 0 while len(a) != n: if r not in a: a.append(r) r = random.randint(1, 9) while s != n: while len(b) != n: x = int(raw_input("予想する数字を大きい桁から1つずつエンターを押して入力してください: ")) if x not in b: b.append(x) print b else: print "同じ数は入力できません" c = set(a) d = set(b) c &= d s = 0 for i in range(n): if a[i] == b[i]: s += 1 print str(len(c) - s) + "Blow" print str(s) + "Hit" b = [] print "おめでとう!"