Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is only one object being called [Python]

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading