테스트 사이트 - 개발 중인 베타 버전입니다

파이썬 질문

파이썬배우기 3년 전 조회 2,463

밑에 있는 사진처럼 코드를 나오게 하고 싶어서 이렇게 코드를 짜봤는데 실행을 하면 계속 오류가 뜹니다. 뭐가 잘못된건지 알려주실 수 있으실까요

class Person:

    def __init__(self, name, mobile=None, email=None):

        self.__name = name

        self.__mobile = mobile

        self.__email = email

 

    def __str__(self):

        p = "< Name: " + str(self.__name) + " >\n"

        if self.__mobile != None:

            p += "mobile phone: " + str(self.__mobile) + "\n"

        if self.__email != None:

            p += "email address: " + str(self.__email) + "\n"

        return p

 

    #접근자

    def getName(self):

        return self.__name

    def getMobile(self):

        return self.__mobile

    def getEmail(self):

        return self.__email

   

    #설정자

    def setName(self, n):

        self.__name = n

    def setMobile(self, m):

        self.__mobile = m

    def setEmail(self, e):

        self.__email = e

 

class Phonebook:

    def __init__(self):

        self.__contacts = dict()

 

    def __str__(self):

        p = ''

        for who in sorted(self.__contacts):

            p += str(self.__contacts[who]) + "\n"

        return p

 

    def add(self, name, mobile=None, email=None):

        who = Person(name, mobile, email)

        self.__contacts[name] = who

 

def main7():

    print("no.7\n")

    obj = Phonebook()

    obj.add("Kim", mobile="010-1213-4567", email="kim@gmail.com")

    obj.add("lee", mobile="010-1213-4568", email="lee@gmail.com")

    obj.add("park", mobile="010-1213-4569", email="park@gmail.com")

    print(obj)

댓글을 작성하려면 로그인이 필요합니다.

답변 1개

플래토
3년 전

올려주신코드를 실행해보면

 

</p>

<p>no.7</p>

<p>< Name: Kim >

mobile phone: *** 개인정보보호를 위한 대폰번호 노출방지 ***

email address: *** 개인정보보호를 한 이메일주소 노출방지 ***</p>

<p>< Name: lee >

mobile phone: *** 개인정보보호를 위한 대폰번호 노출방지 ***

email address: *** 개인정보보호를 한 이메일주소 노출방지 ***</p>

<p>< Name: park >

mobile phone: *** 개인정보보호를 위한 대폰번호 노출방지 ***

email address: *** 개인정보보호를 한 이메일주소 노출방지 ***</p>

<p>

 

와 같은 결과가 나옵니다.

 

아.. 맨아래에

 

main7()을 추가했습니다.

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인