Python Forum
rework of a little python-script - extending getters and setters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
rework of a little python-script - extending getters and setters
#1
hello dear all,

fairly new to python

with this little script i am trying to learn some steps reagarding oo programming.

i want to extend this little script to gather some more information:


01	#!/usr/bin/env python3
02	"""
03	contacts.py
04	This program uses a Person class to keep track of contacts.
05	"""
06	
07	class Person(object):
08	    """
09	    The Person class defines a person in terms of a
10	    name, phone number, and email address.
11	    """
12	    # Constructor
13	    def __init__(self, theName, thePhone, theEmail):
14	        self.name = theName
15	        self.phone = thePhone
16	        self.email = theEmail
17	    # Accesser Methods (getters)
18	    def getName(self):
19	        return self.name
20	    def getPhone(self):
21	        return self.phone
22	    def getEmail(self):
23	        return self.email
24	    # Mutator Methods (setters)
25	    def setPhone(self, newPhoneNumber):
26	        self.phone = newPhoneNumber
27	    def setEmail(self, newEmailAddress):
28	        self.email = newEmailAddress
29	    def __str__(self):
30	        return "Person[name=" + self.name + \
31	               ",phone=" + self.phone + \
32	               ",email=" + self.email + \
33	               "]"
34	
35	def enter_a_friend():
36	    name = input("Enter friend's name: ")
37	    phone = input("Enter phone number: ")
38	    email = input("Enter email address: ")
39	    return Person(name, phone, email)
40	
41	def lookup_a_friend(friends):
42	    found = False
43	    name = input("Enter name to lookup: ")
44	    for friend in friends:
45	        if name in friend.getName():
46	            print(friend)
47	            found = True
48	    if not found:
49	        print("No friends match that term")
50	
51	def show_all_friends(friends):
52	    print("Showing all contacts:")
53	    for friend in friends:
54	        print(friend)
55	
56	def main():
57	    friends = []
58	    running = True
59	    while running:
60	        print("\nContacts Manager")
61	        print("1) new contact    2) lookup")
62	        print("3) show all       4) end ")
63	        option = input("> ")
64	        if option == "1":
65	            friends.append(enter_a_friend())
66	        elif option == "2":
67	            lookup_a_friend(friends)
68	        elif option == "3":
69	            show_all_friends(friends)
70	        elif option == "4":
71	            running = False
72	        else:
73	            print("Unrecognized input. Please try again.")
74	    print("Program ending.")
75	
76	if __name__ == "__main__":
77	    main()
78	
79	
what is wanted... to gather information regarding

    # Constructor

    def __init__(self, theName, thePhone, theEmail, theAdress, ):
        self.name = theName
        self.phone = thePhone
        self.email = theEmail
        self.Adress = theAdress
 
but untill now i have no luck... i only am able to gather inforamtion about the following aspects

    # Constructor
    def __init__(self, theName, thePhone, theEmail, theAdress, ?
        self.name = theName
        self.phone = thePhone
        self.email = theEmail
and the newly added adress - it does not get in effect..

        self.Adress = theAdress
- see my approach:

#!/usr/bin/env python3
"""
contacts.py
This program uses a Person class to keep track of contacts.
@author Richard White
@version 2016-07-30
"""

class Person(object):
    """
    The Person class defines a person in terms of a
    name, phone number, and email address.
    """
    # Constructor
    def __init__(self, theName, thePhone, theEmail, theAdress, ):
        self.name = theName 
        self.phone = thePhone
        self.email = theEmail
        self.adress = theAdress
    # Accesser Methods (getters)
    def getName(self):
        return self.name
    def getPhone(self):
        return self.phone
    def getEmail(self):
        return self.email
    def getAdress(self):
        return self.adress;
    # Mutator Methods (setters)
    def setPhone(self, newPhoneNumber):
        self.phone = newPhoneNumber
    def setEmail(self, newEmailAddress):
        self.email = newEmailAddress
    def __str__(self):
        return "Person[name=" + self.name + \
               ",phone=" + self.phone + \
               ",email=" + self.email + \
               ",adress=" + self.adress + \
               "]"

def enter_a_friend():
    name = input("Enter friend's name: ")
    phone = input("Enter phone number: ")
    email = input("Enter email address: ")
    return Person(name, phone, email)

def lookup_a_friend(friends):
    found = False
    name = input("Enter name to lookup: ")
    for friend in friends:
        if name in friend.getName():
            print(friend)
            found = True
    if not found:
        print("No friends match that term")

def show_all_friends(friends):
    print("Showing all contacts:")
    for friend in friends:
        print(friend)

def main():
    friends = []
    running = True
    while running:
        print("\nContacts Manager")
        print("1) new contact    2) lookup")
        print("3) show all       4) end ")
        option = input("> ")
        if option == "1":
            friends.append(enter_a_friend())
        elif option == "2":
            lookup_a_friend(friends)
        elif option == "3":
            show_all_friends(friends)
        elif option == "4":
            running = False
        else:
            print("Unrecognized input. Please try again.")
    print("Program ending.")

if __name__ == "__main__":
    main()
well - i guess that i have to look and i have to try to find out what goes wrong...
Again:


what is wanted... to gather information regarding

    # Constructor

    def __init__(self, theName, thePhone, theEmail, theAdress, ):
        self.name = theName
        self.phone = thePhone
        self.email = theEmail
        self.Adress = theAdress
 
but untill now i have no luck... i only am able to gather inforamtion about the following aspects

    # Constructor
    def __init__(self, theName, thePhone, theEmail, theAdress, ?
        self.name = theName
        self.phone = thePhone
        self.email = theEmail
and the newly added adress - it does not get in effect..

        self.Adress = theAdress
love to hear from you
Wordpress - super toolkits a. http://wpgear.org/ :: und b. https://github.com/miziomon/awesome-wordpress :: Awesome WordPress: A curated list of amazingly awesome WordPress resources and awesome python things https://github.com/vinta/awesome-python
Reply


Messages In This Thread
rework of a little python-script - extending getters and setters - by apollo - Oct-29-2018, 10:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Extending list doesn't work as expected mmhmjanssen 2 1,434 May-09-2024, 05:39 PM
Last Post: Pedroski55
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 8,224 Jun-29-2023, 11:57 AM
Last Post: gologica
  Can property getters and setters have additional arguments? pjfarley3 2 4,289 Oct-30-2020, 12:17 AM
Last Post: pjfarley3
  How to kill a bash script running as root from a python script? jc_lafleur 4 8,103 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 3,016 May-28-2020, 05:27 PM
Last Post: micseydel
  Package python script which has different libraries as a single executable or script tej7gandhi 1 3,278 May-11-2019, 08:12 PM
Last Post: keames
  Extending my text file word count ranker and calculator Drone4four 8 7,402 Jan-25-2019, 08:25 AM
Last Post: steve_shambles
  Check Python version from inside script? Run Pythons script in v2 compatibility mode? pstein 2 10,733 Jul-07-2017, 08:59 AM
Last Post: snippsat
  getters and setters Skaperen 10 10,414 May-16-2017, 07:13 AM
Last Post: Ofnuts
  Cant pass corect variables to python script in bash script neradp 3 7,315 Nov-05-2016, 01:26 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020