본문 바로가기
Python/Python 기초

파이썬 야구게임 예제

by 미눅스[멘토] 2023. 6. 27.
728x90

번호 맞추기

from random import random

def getS(com, mine):

    ret = 0
    c1 = com[0:1]
    c2 = com[1:2]
    c3 = com[2:3]
    
    m1 = mine[0:1]
    m2 = mine[1:2]
    m3 = mine[2:3]
    
    if c1== m1 :
        ret += 1
    if c2== m2 :
        ret += 1
    if c3== m3 :
        ret += 1
        
    return ret

def getB(com, mine):

    ret = 0
    c1 = com[0:1]
    c2 = com[1:2]
    c3 = com[2:3]
    
    m1 = mine[0:1]
    m2 = mine[1:2]
    m3 = mine[2:3]
    
    if c2==m1 or c3==m1 :
        ret += 1
    if c1==m2 or c3==m2 :
        ret += 1
    if c2==m3 or c1==m3 :
        ret += 1
        
    return ret

def ranC():
    arr = [1,2,3,4,5,6,7,8,9]
    
    for i in range(100) :
        rnd = int(random()*9)
        a=arr[0]
        arr[0] = arr[rnd]
        arr[rnd] = a
    
    ret =str(arr[1])+str(arr[2])+str(arr[3])
    return ret


com = ranC()

# s = getS(com, mine)
# b = getB(com,mine)
print(com)//이건 내가 정답 맞추기용

while True:
    mine = input("3자리 숫자를 맞추세요")
    s = getS(com, mine)
    b = getB(com,mine)
    print(s,"s",b,"b","입니다.")
    
    
    if s==3 :
        print(com,"정답입니다.!!")
        break

결과