-1

I want to create a program which will create 3 instances of a class and store different values for different people.

The current code that I have will output the last person's information. I do not know if I correctly set up a class or not but any help is much needed.

#Main module
def main():
    #Declare variables
    name = ""
    address = ""
    age = ""
    phoneNumber = ""
    person1Information = personalInformation(name, address, age, phoneNumber)
    person2Information = personalInformation(name, address, age, phoneNumber)
    person3Information = personalInformation(name, address, age, phoneNumber)

    #Person 1
    name, address, age, phoneNumber = getPersonInformation(name, address, age, phoneNumber)
    storePerson1Information(person1Information, name, address, age, phoneNumber)
    #Person 2
    name, address, age, phoneNumber = getPersonInformation(name, address, age, phoneNumber)
    storePerson2Information(person2Information, name, address, age, phoneNumber)
    #Person 3
    name, address, age, phoneNumber = getPersonInformation(name, address, age, phoneNumber)
    storePerson3Information(person3Information, name, address, age, phoneNumber)

    #Display
    displayInformation(person1Information, person2Information, person3Information)

#Ask for information
def getPersonInformation(name, address, age, phoneNumber):
    name = str(input("What is this person's name?"))
    address = str(input("What is the address for this person?"))
    age = int(input("What is this person's age?"))
    phoneNumber = str(input("What is their phone number?"))
    return name, address, age, phoneNumber

#Add peron 1's information to class
def storePerson1Information(person1Information, name, address, age, phoneNumber):
    person1Information.setName(name)
    person1Information.setAddress(address)
    person1Information.setAge(age)
    person1Information.setPhoneNumber(phoneNumber)
    return

#add peron 2's information to class
def storePerson2Information(person2Information, name, address, age, phoneNumber):
    person2Information.setName(name)
    person2Information.setAddress(address)
    person2Information.setAge(age)
    person2Information.setPhoneNumber(phoneNumber)
    return

#Add person 3's information to class
def storePerson3Information(person3Information, name, address, age, phoneNumber):
    person3Information.nameInformation = name
    person3Information.addressInformation = address
    person3Information.ageInformation = age
    person3Information.phoneNumberInformation = phoneNumber
    return

#Display the results
def displayInformation(person1Information, person2Information, person3Information):
    print("Person 1's name:", person1Information.getName())
    print("Person 2's name:", person2Information.getName())
    print("Person 3's name:", person3Information.getName())
    print("Person 1's age:", person1Information.getAge())
    print("Person 2's age:", person2Information.getAge())
    print("Person 3's age:", person3Information.getAge())
    print("Person 1's address:", person1Information.getAddress())
    print("Person 2's address:", person2Information.getAddress())
    print("Person 3's address:", person3Information.getAddress())
    print("Person 1's phone number:", person1Information.getPhoneNumber())
    print("Person 2's phone number:", person2Information.getPhoneNumber())
    print("Person 3's phone number:", person3Information.getPhoneNumber())

#Class holding variables and methods
class personalInformation(object):
    #Declare vaibales
    nameInformation = ""
    addressInformation = ""
    ageInformation = 0
    phoneNumberInformation = ""
    #Initialize?
    def __init__(self, nameInformation, addressInformation, ageInformation, phoneNumberInformation):
        self.nameInformation = nameInformation
        self.addressInformation = addressInformation
        self.ageInformation = ageInformation
        self.phoneNumberInformation = phoneNumberInformation
    #Modifiers
    def setName(self, name):
        nameInformation = name
        return
    def setAddress(self, address):
        addressInformation = address
        return
    def setAge(self, age):
        ageInformation = age
        return
    #Accessor
    def setPhoneNumber(self, phoneNumber):
        phoneNumberInformation = phoneNumber
    def getName(self):
        return self.nameInformation
    def getAge(self):
        return self.ageInformation
    def getAddress(self):
        return self.addressInformation
    def getPhoneNumber(self):
        return self.phoneNumberInformation

#Call main module
main()
Citizen
  • 12,430
  • 26
  • 76
  • 117
iProfiter
  • 3
  • 1
  • You should read up on [class vs instance variables](https://stackoverflow.com/questions/2714573/instance-variables-vs-class-variables-in-python). You are creating variables that belong to *all members of your class* instead of *each member* – Cory Kramer Dec 03 '16 at 21:45
  • Also you should [avoid setters and getters in Python](https://stackoverflow.com/questions/2627002/whats-the-pythonic-way-to-use-getters-and-setters) – Cory Kramer Dec 03 '16 at 21:46
  • And you should learn how to use the same set of functions three times (for the three persons), not three near-identical copies. – alexis Dec 03 '16 at 21:54

1 Answers1

0

Basically I think you're right. I don't know if my answer is what you're looking for, but I believe that all you have to do is something like this:

Code:

class Person:

    def __init__(self, name):
        self.name = name
    def setName(self, name):
        self.name = name
    def getName(self):
        return self.name

def main():

    person1 = Person("John")
    person2 = Person("Fred")
    person3 = Person("Paul")

    print person1.name
    print person2.name
    print person3.name

main()

It's really simple and it works. If you need it, you can introduce more fields in the defintion of Person init. Is this what you were asking for?

  • Yes thank you. I found that when I declared the person1Information variable after the getPersonInformation() function was called, everything displayed correctly. – iProfiter Dec 03 '16 at 22:34