Skip to content

Instantly share code, notes, and snippets.

@mojaie
Created January 8, 2012 16:16
Show Gist options
  • Save mojaie/1578863 to your computer and use it in GitHub Desktop.
Save mojaie/1578863 to your computer and use it in GitHub Desktop.
hitandblow.py
# coding: UTF-8
import random
print '###Hit and Blow###'
ls = range(10)
random.shuffle(ls)
answer = ls[:4]
while 1:
try:
nums = raw_input('0-9の整数を4つ入力してください(空白区切り):')
numarr = [int(e) for e in nums.split(' ') if len(e) == 1]
if len(numarr) != 4:
raise
except:
print 'エラー:入力が不正です。'
continue
hit = 0
blow = 0
for i in range(4):
if answer[i] == numarr[i]:
hit += 1
continue
for j in range (4):
if answer[i] == numarr[j]:
blow += 1
print str(hit) + ' hit ' + str(blow) + ' blow'
if hit == 4:
print 'Congratulations!'
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment