在python中以不同的字符串长度均匀地隔开输出数据

2024-05-19 12:36:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我在写这个代码。一切都是完美的,但我坚持让输出匹配的间距方面

输出应该是

    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()

Tags: theselfreturntitlemydefcellfirstname
1条回答
网友
1楼 · 发布于 2024-05-19 12:36:40

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

更改:

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

收件人:

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

相关问题 更多 >