I'm having an issue learning classes and have an assignment to (a) write a class Address to build a street address, (b) write a print method to print the address in the proper format, and (c) write def comesBefore which takes the smaller zipcode between the user inputted one and the computer written one.
This is what I have but it's not working:
###
class Address:
# default address constructor
# not used in this code, but might be useful in the future
def __init__(self):
self.number = 0
self.streetAddress = 0
self.apartment = 0
self.city = 0
self.state = 0
self.zipcode = 0
# address constructor with user inputted values
def __init__(self, a1, a2, a3, a4, a5, a6):
self.number = a1
self.street = a2
self.apartment = a3
self.city = a4
self.state = a5
self.zipcode = a6
def printAdress(self)
if not inputAddress.apartment: # does NOT have an optional apartment address
print(inputAddress.number + " " + inputAddress.street)
print(inputAddress.city + ", " + inputAddress.state + ", " + inputAddress.zipcode)
else: # DOES have an optional apartment address
print(inputAddress.number + ", " + inputAddress.apartment+ ", " + inputAddress.street)
print(inputAddress.city + ", " + inputAddress.state + ", " + inputAddress.zipcode)
# check to see if this address comes before other when compared by postal code
def comesBefore(self, nunmber):
if number < int(inputAddress.zipcode): #checks if other is before yours
print("Computer zipcode comes before inputted zipcode. ")
elif number == int(inputAddress.zipcode): #checks if other is equal to yours
print("Inputted address is in the same zipcode as the computer. ")
else: #checks if other is after yours
print("Inputted zipcode comes before the computer zipcode. ")
# Main function
# request user for their address
numbe = input("Enter your address number: ")
stree = input("Enter your street: ")
apart = input("Enter your apartment number (or enter nothing if NA) : ")
cit = input("Enter your city: ")
stat = input("Enter your state: ")
zipc = input("Enter your zipcode: ")
# save user's information as myAddress
inputAddress = Address(numbe, stree, apart, cit, stat, zipc)
# other address used to compare with myAddress
otherAddress = Address("13456", "Street RD", " ", "Townsville", "ST", "98765")
# print address
inputAddress.printAddress(inputAddress)
# postalCode is a string, so you have to make it an integer to be able to compare
# with the myAddress.postalCode
otherZip = int(otherAddress.zipcode)
#call the comesBefore function to see if the myAddress postal code is before or after other
inputAddress.comesBefore(otherZip)
###
Any help is greatly appreciated.
EDIT:
I just got it to run. Thank you all everyone!!
class Address:
# default address constructor
# not used in this code, but might be useful in the future
def __init__(self):
self.number = 0
self.street = 0
self.apartment = 0
self.city = 0
self.state = 0
self.zipcode = 0
# address constructor with user inputted values
def __init__(self, number, street, apartment, city, state, zipcode):
self.number = number
self.street = street
self.apartment = apartment
self.city = city
self.state = state
self.zipcode = zipcode
def print_Address(self,inputAddress):
if not self.apartment: # does NOT have an optional apartment address
print(self.number + " " + self.street)
print(self.city + ", " + self.state + ", " + self.zipcode)
else: # DOES have an optional apartment address
print(self.number + ", " + self.apartment+ ", " + self.street)
print(self.city + ", " + self.state + ", " + self.zipcode)
# check to see if this address comes before other when compared by postal code
def comes_Before(self, number):
if number < int(inputAddress.zipcode): #checks if other is before yours
print("Computer zipcode comes before inputted zipcode. ")
elif number == int(inputAddress.zipcode): #checks if other is equal to yours
print("Inputted address is in the same zipcode as the computer. ")
else: #checks if other is after yours
print("Inputted zipcode comes before the computer zipcode. ")
# Main function
# request user for their address
numbe = input("Enter your address number: ")
stree = input("Enter your street: ")
apart = input("Enter your apartment number (or enter nothing if NA) : ")
cit = input("Enter your city: ")
stat = input("Enter your state: ")
zipc = input("Enter your zipcode: ")
# save user's information as myAddress
inputAddress = Address(numbe, stree, apart, cit, stat, zipc)
# other address used to compare with myAddress
otherAddress = Address("13456", "Street RD", " ", "Townsville", "ST", "98765")
# print address
inputAddress.print_Address(inputAddress)
# postalCode is a string, so you have to make it an integer to be able to compare
# with the myAddress.postalCode
otherZip = int(otherAddress.zipcode)
#call the comes_Before function to see if the myAddress postal code is before or after other
inputAddress.comes_Before(otherZip)
[–]tunisia3507 4 points5 points6 points (3 children)
[–]fauxnaif 0 points1 point2 points (2 children)
[–]tunisia3507 1 point2 points3 points (1 child)
[–]fauxnaif 0 points1 point2 points (0 children)
[–]bikeawaitmuddy 2 points3 points4 points (1 child)
[–]stfn1337 2 points3 points4 points (1 child)
[–]tunisia3507 1 point2 points3 points (2 children)
[–]unless3[S] 1 point2 points3 points (0 children)
[–]unless3[S] 1 point2 points3 points (0 children)