使用 def comeBefore(self, other) 编写按邮政编码对地址进行排序的方法
我有一个任务如下: “提供一个方法 def comesBefore(self, other) 来测试在通过邮政编码进行比较时该地址是否在其他地址之前。该方法应该返回一个布尔值。”
这是为了编写一个收集地址并按邮政编码顺序返回它们的类。他希望我们使用“def comesBefore(self, other)”方法来执行此操作。
我不太擅长Python,所以我一生都不知道如何让它工作,但这就是我所拥有的:
Class Address py
#address class
class Address:
#initializing
def __init__(self, house_street = '', apt_number = '', city = '', state = '', postal_code = ''):
self.set_house_street(house_street)
self.set_city(city)
self.set_state(state)
self.set_postal_code(postal_code)
self.set_apt_number(apt_number)
def __str__(self): #Function to tell str how to deal with Address
return self.get_full_address()
#set and capitalize house number and street name
def set_house_street(self, house_street):
self._house_street = house_street.title()
#return
def get_house_street(self, house_street):
return self._house_street
#optional apt number
def set_apt_number(self, apt_number = ''): # <- Se a default parameter
self._apt_number = apt_number
#get and return apt number
def get_apt_number(self, apt_number):
return self._apt_number
#set and capitalize city
def set_city(self, city):
self._city = city.capitalize()
#return city
def get_city(self, city):
return self._city
#set and capitalize state and only keep the first 2 letters
def set_state(self, state):
self._state = state[0:2].upper()
#return state
def get_state(self, state):
return self._state
#set postal code
def set_postal_code(self, postal_code):
self._postal_code = postal_code
#return postal code
def get_postal_code(self, postal_code):
return self._postal_code
#return the full address
def get_full_address(self):
if self._apt_number == '':
full_address = f'{self._house_street}\n{self._city}, {self._state} {self._postal_code}'
else:
full_address = f'{self._house_street}, Apt {self._apt_number}\n{self._city}, {self._state} {self._postal_code}'
return full_address
#here's where I'm having trouble.
#I've never had to use 'other' before so I'm not quite sure how to properly use it.
#It's supposed to compare the postal codes and sort the addresses in order according to the postal code.
#This bit of code for this function is what my tutor gave me to work with, and I'm still having trouble understanding it ;-;
def comesBefore(self, other):
retVal = False
if (self._postal_code < postal_code):
retVal = True
elif (self.full_address < full_address):
retVal = True
return retVal
Project 4 Test py
from Class_Address import Address
#finding out how many people they want to get the info from
def get_how_many(title):
#not done is considered true right now, which means we aren't done getting addresses to get info from
not_done = True
#so while we're not done, we make a try/except program
while not_done:
#trying to
try:
#get a quanity of 'how many address are we getting?'
qty = int(input(title))
#if they input less than 0, we're done and don't need to do anything
if qty > 0:
not_done = False
#otherwise, if they do input <1, display the error
else:
print('You must have at least 1 data point to enter. Please try again.')
#otherwise, print an exception
except:
print('Invalid input! Please try again.')
#return the quantity of addresses
return qty
#making a list to hold info
def getting_address(how_many):
#empty list
address_list = []
#for each person they want to get info from
for i in range(how_many):
#request addresses
houseStreet = input("What is your address? ")
aptNum = input("What is your apartment number? ")
city = input("What is your city? ")
state = input("What is your state? ")
postal_code = input("What is your 5 digit postal code? ")
address_list.append(Address(houseStreet, aptNum, city, state, postal_code)) #<- Need to add to your list
#return list
return address_list
#print addresses
def print_address(address_list):
print("Here are the addresses you've entered:")
for address in address_list:
# print(f'{address.get_house_street()} Apt{address.get_apt_number}')
# print(f'{address.get_city()}, {address.get_state()} {address.get_postal_code()}')
print(str(address)) #<- Get a string version of hte address and print it.
#main
def main():
how_many = get_how_many("How many addresses would you like to enter? ")
address_list = getting_address(how_many)
print_address(address_list)
#calling main
if __name__ == '__main__':
main()
所有代码都运行,我只是很难实现对地址进行排序的函数通过邮政编码。如果有人能解释如何在两个 py 文件中实现该函数,那就太棒了!
I've got an assignment as follows:
"Supply a method def comesBefore(self, other) that tests whether this address comes before other when compared by postal code. This method should return a boolean value."
This for writing a class that collects the addresses and returns them in order by zip code. He wants us to use the 'def comesBefore(self, other)' method to do so.
I'm not great at python so for the life of me I can't figure out how to get it to work, but here's what I have:
Class Address py
#address class
class Address:
#initializing
def __init__(self, house_street = '', apt_number = '', city = '', state = '', postal_code = ''):
self.set_house_street(house_street)
self.set_city(city)
self.set_state(state)
self.set_postal_code(postal_code)
self.set_apt_number(apt_number)
def __str__(self): #Function to tell str how to deal with Address
return self.get_full_address()
#set and capitalize house number and street name
def set_house_street(self, house_street):
self._house_street = house_street.title()
#return
def get_house_street(self, house_street):
return self._house_street
#optional apt number
def set_apt_number(self, apt_number = ''): # <- Se a default parameter
self._apt_number = apt_number
#get and return apt number
def get_apt_number(self, apt_number):
return self._apt_number
#set and capitalize city
def set_city(self, city):
self._city = city.capitalize()
#return city
def get_city(self, city):
return self._city
#set and capitalize state and only keep the first 2 letters
def set_state(self, state):
self._state = state[0:2].upper()
#return state
def get_state(self, state):
return self._state
#set postal code
def set_postal_code(self, postal_code):
self._postal_code = postal_code
#return postal code
def get_postal_code(self, postal_code):
return self._postal_code
#return the full address
def get_full_address(self):
if self._apt_number == '':
full_address = f'{self._house_street}\n{self._city}, {self._state} {self._postal_code}'
else:
full_address = f'{self._house_street}, Apt {self._apt_number}\n{self._city}, {self._state} {self._postal_code}'
return full_address
#here's where I'm having trouble.
#I've never had to use 'other' before so I'm not quite sure how to properly use it.
#It's supposed to compare the postal codes and sort the addresses in order according to the postal code.
#This bit of code for this function is what my tutor gave me to work with, and I'm still having trouble understanding it ;-;
def comesBefore(self, other):
retVal = False
if (self._postal_code < postal_code):
retVal = True
elif (self.full_address < full_address):
retVal = True
return retVal
Project 4 Test py
from Class_Address import Address
#finding out how many people they want to get the info from
def get_how_many(title):
#not done is considered true right now, which means we aren't done getting addresses to get info from
not_done = True
#so while we're not done, we make a try/except program
while not_done:
#trying to
try:
#get a quanity of 'how many address are we getting?'
qty = int(input(title))
#if they input less than 0, we're done and don't need to do anything
if qty > 0:
not_done = False
#otherwise, if they do input <1, display the error
else:
print('You must have at least 1 data point to enter. Please try again.')
#otherwise, print an exception
except:
print('Invalid input! Please try again.')
#return the quantity of addresses
return qty
#making a list to hold info
def getting_address(how_many):
#empty list
address_list = []
#for each person they want to get info from
for i in range(how_many):
#request addresses
houseStreet = input("What is your address? ")
aptNum = input("What is your apartment number? ")
city = input("What is your city? ")
state = input("What is your state? ")
postal_code = input("What is your 5 digit postal code? ")
address_list.append(Address(houseStreet, aptNum, city, state, postal_code)) #<- Need to add to your list
#return list
return address_list
#print addresses
def print_address(address_list):
print("Here are the addresses you've entered:")
for address in address_list:
# print(f'{address.get_house_street()} Apt{address.get_apt_number}')
# print(f'{address.get_city()}, {address.get_state()} {address.get_postal_code()}')
print(str(address)) #<- Get a string version of hte address and print it.
#main
def main():
how_many = get_how_many("How many addresses would you like to enter? ")
address_list = getting_address(how_many)
print_address(address_list)
#calling main
if __name__ == '__main__':
main()
All the code runs, I'm just having difficulty implementing the function to sort the addresses by postal code. If anyone could explain how to implement the function into the two py files, that'd be absolutely wonderful!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论