• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

AttributeError: 'set' object has no attribute 'count'

20.05.08 19:06 작성 조회수 929

1

오류떠요

import numpy

def make_lotto_number(**kwargs):
    # rand_number = numpy.random.choice(range(1, 46), 6 , replace=False)
    rand_number = [123456]
    rand_number.sort()
    lotto = []

    if kwargs.get("include"):
        include = kwargs.get("include")
        lotto.extend(include)

        cnt_make = 6 - len(lotto)

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

    if kwargs.get("exclude"):
        exclude = kwargs.get("exclude")
        lotto = set(lotto) - set(exclude)

        while len(lotto) != 6:
            for _ in range(6 - len(lotto)):
                rand_number = numpy.random.choice(range(146), 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
    lotto.sort()
    return lotto
       
print(make_lotto_number(exclude=[123]))

답변 1

답변을 작성해보세요.

0

강좌내용에서 놓치신게 있으셔서 그렇습니다.

lotto = []

위에서 처럼 lotto = []  는 리스트의 자료 형태를 유지해야 하는데.. 올려주신 코드를 보면

lotto = set(lotto) - set(exclude)

위처럼 set 으로 집합형태로 변환하신 후 다시 리스트로 캐스팅 되지 않아서 집합형태의 자료형에서 count 라는 함수가 없어서 오류가 발생하는 겁니다.

lotto = list(set(lotto) - set(exclude))

따라서 위처럼 코드를 수정하셔야 합니다.