在python中用不同的字符串长度均匀间隔输出数据

时间:2018-10-17 04:42:49

标签: python python-3.x

我正在编写此代码。一切都很完美,但是一直坚持让输出在间距上匹配

输出应该是

    My Contacts
    -----------
    Homer Simpson       406-994-0000
    ???? Simpson        406-994-5959
    Amanda Huginkiss    406-994-4780
    -----------

    My Contacts
    -----------
    Homer Simpson       406-994-0000
    Bart Simpson        406-994-5959
    Amanda Huginkiss    406-994-4780
    -----------

    The area code for cell number 406-994-0000 is 406

但是我得到

    My Contacts
    -----------
    Homer Simpson            406-994-0000
    ???? Simpson             406-994-5959
    Amanda Huginkiss                 406-994-4780
    -----------

    My Contacts
    -----------
    Homer Simpson            406-994-0000
    Bart Simpson             406-994-5959
    Amanda Huginkiss                 406-994-4780
    -----------

    The area code for cell number 406-994-0000 is 406

我意识到这是因为第三位联系人的姓名更长,但我希望空格相同。这是姓名和电话号码之间的空格

这是代码

#contact class
class Contact:

    #The constructor of the Contact class
    def __init__(self,firstname,lastname,contact):
        self.firstname=firstname
        self.lastname=lastname
        self.cell_num=contact
        self.title=''

    #set the firstname
    def setFirstName(self,firstname):
        self.firstname=firstname

    #get the firstname
    def getFirstName(self):
        return self.firstname

    #set the lastname   
    def setLastName(self,lastname):
        self.lastname=lastname

    # get the lastname
    def getLastName(self):
        return self.lastname

    #set the cell number
    def setCellNumber(self,cellnumber):
        self.cellNum=cellnumber

    # method to get the cell number
    def getCellNumber(self):
        return self.cellNum   

    #get the area code
    def getAreaCode(self):
        return self.cellNum[:3]

    #set the area code
    def setAreaCode(self,areacode):
        self.cell_num=str(areacode)+self.cell_num[3:]

    #set title
    def setTitle(self,title):
        self.title=title+' '

    #get title
    def getTitle(self):
        return self.title[:-1]


    #print the object
    def __str__(self):
        return "%s %s \t\t %s" % (self.firstname, self.lastname, self.cell_num)

    #used by print_directory function
    def printEntry(contacts):
            print(contacts)


# -----------------------------------------------------

def print_directory(contacts):
    print("My Contacts")
    print("-----------")
    for person in contacts:     
        person.printEntry()
    print("-----------\n")

# -----------------------------------------------------

def main():
    dad = Contact("Homer", "Simpson", "406-994-0000")
    son = Contact("????", "Simpson", "406-994-5959")
    pun = Contact("Amanda", "Huginkiss", "406-994-4780")

    contacts = [dad, son, pun]

    print_directory(contacts)

    son.setFirstName("Bart")
    dad.setTitle("Dad")
    pun.setTitle("Silly Pun")

    print_directory(contacts)

    print("The area code for cell number", dad.getCellNumber(), "is", \
           dad.getAreaCode())

# -----------------------------------------------------

main()

1 个答案:

答案 0 :(得分:1)

您应该在格式字符串中为人名指定宽度,而不要使用制表符:

更改:

return "%s %s \t\t %s" % (self.firstname, self.lastname, self.cell_num)

收件人:

return "%-20s%s" % (' '.join((self.firstname, self.lastname)), self.cell_num)