So I am working on a piece of code that creates an object and then calls a method to make the address but whenever I go to run it only the most recent object is called, if I make 2 objects only the second object created will be used
class Address:
def __init__(self, hnum, sname, city, state, pcode):
self.hnum = hnum
self.sname = sname
self.city = city
self.state = state
self.pcode = pcode
def printAddress(self):
"""
gets address and prints it
"""
return self.hnum + " " + self.sname + ", " + self.city + ", " + self.state + " " + self.pcode
addressObj1 = Address("2/15", "John Ave", "Sydney", "NSW", "2512")
addressObj2 = Address("3", "Circle St", "Mathville", "ACT", "0214")
addressObj1.printAddress()
addressObj2.printAddress()
This is the code I used along with the objects I made for it if anyone can help that would be appreciated.
>Solution :
The PrintAddress function just returns a value. Nowhere in your code do you call the print function to actually print the address out to the screen. So print it out like below.
def printAddress(self):
"""
gets address and prints it
"""
print(self.hnum + " " + self.sname + ", " + self.city + ", " + self.state + " " + self.pcode)