인공지능 공부/남박사의 파이썬 실전

(인프런) 09. 실전파이썬 스마트 로또번호추첨기

'''
1. 특정 숫자를 포함해서 로또번호를 생성해주는 기능
2. 특정 숫자를 제외해서 로또번호를 생성해주는 기능
3. 정해진 자리수만큼 연속 숫자를 포함하는 번호를 생성하는 기능
'''

import numpy
import random
import os

os.system("cls")

def make_lotto_number(**kwargs): ##사용자가 옵션을 줄 수도 있기 때문에 
    ##numpy 사용하면 1~46사이에 6가지 숫자를 replace는 중복되지 않게
    ##그러면 rand_number은 리스트형태로 담아줌 
    rand_number = numpy.random.choice(range(1,46), 6, replace = False)
    rand_number.sort()

    ##최종 로또번호가 완성될 변수
    lotto = []

    if kwargs.get("include"):
        include = kwargs.get("include")
        ##append 는 하나씩 추가한다 extend는 리스트형태가 아닌 확장형태로 들어가서 이렇게 사용함
        lotto.extend(include)
        cnt_make = 6 - len(lotto)

        for _ in range(cnt_make):
            for j in rand_number:
                if lotto.count(j) ==0:
                    lotto.append(j)
                    break
    else : 
        lotto.extend(rand_number)

##여기는 2. 특정 숫자를 제외해서 로또번호를 생성해주는 기능
    if kwargs.get("exclude"):
        exclude = kwargs.get("exclude")
        lotto = list(set(lotto) - set(exclude)) ##집합은 중복을 허용하지 않음  차집합임!

        while len(lotto) != 6:
            for _ in range(6-len(lotto)):
                rand_number = numpy.random.choice(range(1,46), 6, replace = False)
                rand_number.sort()

                for j in rand_number:
                    if lotto.count(j) == 0 and j not in exclude:
                        lotto.append(j)
                        break
    ## 3. 정해진 자리수만큼 연속 숫자를 포함하는 번호를 생성하는 기능
    if kwargs.get("continuty"):
        continuty = kwargs.get("continuty")
        start_number = numpy.random.choice(lotto, 1)
        
        seq_num = []
        for i in range(start_number[0], start_number[0] + continuty):
            seq_num.append(i)
        seq_num.sort()
        cnt_make = 6-len(seq_num)
        lotto = []

        lotto.extend(seq_num)

        while len(lotto) !=6:
            for _ in range(6-len(lotto)):
                 rand_number = numpy.random.choice(range(1,46), 6, replace = False)
                 rand_number.sort()

                 for j in rand_number:
                     if lotto.count(j) == 0 and j not in seq_num:
                         lotto.append(j)
                         break

                 lotto = list(set(lotto))




    lotto.sort()
    return lotto

print(make_lotto_number(include =[1,2]))
print(make_lotto_number(exclude =[3,4]))
print(make_lotto_number(continuty = 3))
    ##make_lotto_number(include=[1,2]) 이런식으로 받으려고