#야구게임
from random import random
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in range(1000): # 섞기
rnd = int(random()*(8))+1
temp = arr[0]
arr[0] = arr[rnd]
arr[rnd] = temp
com = [arr[0], arr[1], arr[2]] # 3자리수 저장
# print(com)
while True:
mine = input("숫자를 입력해주세요")
mineArr = list(mine) # 순서를 위해 배열로 분리
strkie = 0
ball = 0
for cdx, c in enumerate(com) :
for mdx, m in enumerate(mineArr) :
if mdx==cdx and int(c)==int(m) : # strkie일 때
strkie+=1
elif int(c)==int(m) : # ball일 때
ball+=1
if strkie == 3 :
print(mine, strkie,"S",ball,"B","축하합니다") # 정답
break # 종료
print(mine, strkie,"S",ball,"B") # 틀림
substring을 이용한 풀이
#야구게임
from random import random
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in range(1000): # 섞기
rnd = int(random()*(8))+1
temp = arr[0]
arr[0] = arr[rnd]
arr[rnd] = temp
def getS(com, mine):
ret = 0
for i in range(0,3) :
c = com[i:i+1] #substring
m = mine[i:i+1]
if c==m :
ret += 1
return ret
def getB(com, mine):
ret = 0
for i in range(0,3) :
c = com[i:i+1] #substring
for j in range(0,3) :
m = mine[j:j+1]
if c==m and i!=j :
ret += 1
return ret
com = "{}{}{}".format(arr[0],arr[1],arr[2])
print(com)
while True:
mine = input("숫자를 입력해주세요")
strkie = getS(com, mine)
ball = getB(com, mine)
if strkie == 3 :
print(mine, strkie,"S",ball,"B","축하합니다") # 정답
break # 종료
print(mine, strkie,"S",ball,"B") # 틀림