gpt4 book ai didi

python - 在python中均匀分布具有不同字符串长度的输出数据

转载 作者:行者123 更新时间:2023-12-01 08:54:49 26 4
gpt4 key购买 nike

我正在编写这段代码。一切都很完美,但我坚持让输出在间距方面匹配

输出应该是

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

最佳答案

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

更改:

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

至:

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

关于python - 在python中均匀分布具有不同字符串长度的输出数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52847417/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com