team 과제 - 2

chanloper·2024년 7월 4일

python

목록 보기
9/12
class Member:
    def __init__(self, name, username, password):
        # 생성자 구현
        self.name = name
        self.username = username
        self.password = password

    def display(self):
        # 메소드 구현
        print(f'회원이름:{self.name} 아이디:{self.username}')  # password는 제외


class Post():
    def __init__(self, title, content, author):
        self.title = title
        self.content = content
        self.author = author

    def create(self):
        # 메소드 구현
        print(f'제목:{self.title} 내용:{self.content} 작성자:{self.author.username}')


def display_members(members):  # 함수 추가
    print("--------------------------------------")
    for member in members:
        member.display()  # display 메소드 호출
    print()


def create_post(posts):  # 함수 추가
    # 특정유저가 작성한 게시글의 제목을 모두 프린트 / # 특정 단어가 content에 포함된 게시글의 제목을 모두 프린트
    print("특정유저 'Jenny'가 작성한 게시글의 제목 : ")
    for post in posts:
        if post.author.username == 'Jenny':
            print(post.title)
        # post.create() / # create 메소드 호출
        # print()

    print("--------------------------------------")
    keyword = "특정 단어"  # keyword 란 변수 선언 / 이런 알고리즘이 익숙해져야 한다.
    print(f"게시글 내용에 {keyword}를 포함하는 게시글의 제목 :")
    # for in / if in 으로 post.content 에 keyword("특정 단어")가 들어가 있는 것만 반복문으로 출력
    for post in posts:
        if keyword in post.content:
            print(post.title)

            # 코드 실행
members = []  # 이곳에 .append
posts = []

M1 = Member('John-Wak', 'Wak', 1234)  # Member의 M1 인스턴스 생성
M2 = Member('Kim-Jenny', 'Jenny', 4567)
M3 = Member('Lee-Chunsam', 'Chunsam', 8910)
# M1,M2,M3 세 개의 인스턴스를 빈 리스트에 apeend 하는 방법
members.append(M1)
members.append(M2)
members.append(M3)

# Post 인스턴스 생성 후 , M1(Member) 인스턴스를 넘김 / author 자리에 username을 넘김
P1 = Post("제목1", "특정 단어1", M1)
P2 = Post("제목2", "내용2", M1)
P3 = Post("제목3", "내용3", M1)
P4 = Post("제목4", "내용1", M2)
P5 = Post("제목5", "내용2", M2)
P6 = Post("제목6", "내용3", M2)
P7 = Post("제목7", "특정 단어7", M3)
P8 = Post("제목8", "내용2", M3)
P9 = Post("제목9", "내용3", M3)

posts.append(P1)
posts.append(P2)
posts.append(P3)
posts.append(P4)
posts.append(P5)
posts.append(P6)
posts.append(P7)
posts.append(P8)
posts.append(P9)

display_members(members)
create_post(posts)

# M1.display() / # M1 인스턴스의 display 메소드 호출
# M2.display()

# 인자로 인스턴스를 넘겨줌
# Post 인스턴스를 만들 때 author 자리에 만들어둔 작성자 인스턴스를 넘기기
# P1 = Post("a","b",M1.username) # Post 인스턴스 생성 후, 이름 값을 넘김
P1 = Post("a", "b", M1)  # Post 인스턴스 생성 후 , M1 인스턴스를 넘김

# M1을 넘겨야 Post 안에서 author.username이 가능하구
# M1.username으로 넘기면 Post 안에서 author 자체가 이름 값이 되어버림
  1. 특정 유저가 작성한 게시글의 제목을 모두 프린트
    특정 유저를 'Jenny' 로 설정 후,
    for post in posts:
    if post.author.username == 'Jenny'
    print(post.title)
    Jenny가 작성한 게시글의 제목을 출력되게 만들었다.

  2. 게시글 내용에 특정 단어가 포함된 게시글의 제목을 모두 프린트
    keyword 라는 변수를 만들어 "특정 단어"란 문자열을 넣어준 후,
    for post in posts:
    if keyword in post.content:
    print(post.title)
    post.content에 keyword 가 in (들어가)되있으면
    특정 단어가 포함된 게시글의 제목을 출력되게 만들었다.

기존의 함수가 def create_post(self) 였다면 () 안을 posts로 변경하였다.
posts란 Post 인스턴스들을 append한 리스트다.

과제의 기본 사항들은 완성했으니, 추가 도전 과제들을 추가하거나
이를 응용해서 다른 py 를 만들어 보며 공부를 더 할것 같다.

함수의 사용법이나 변수에 옮겨 담아 사용한다는 개념 자체가
아직 한참 모자르다

반복 학습만이 이를 보완해줄 수 있을것 같다.

profile
이것 뭐에요?

0개의 댓글