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
#5
hi there buran

i fixed the indent issue and changed the class - but there were new issues arising

#!/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, name, phone, email, address):
        self.name = name
        self.phone = phone
        self.email = email
        self.adress = address

    def __str__(self):
        return "Person[name={}, phone={}, email={}, address={}]".format(self.name, self.phone,
                                                                        self.email, self.adress)


# create instance
person = Person(name='John Doe', phone='123454321', email='[email protected]',
                address='1600 Pennsylvania ave., Washington DC')
# access property
print(person.name)

# print object
print(person)

# assign new value of property
person.name = 'Jack Ryan'

# print again
print(person)
    # Accesser Methods (getters)
    def getName(self):
        return self.name
    def getPhone(self):
        return self.phone
    def getEmail(self):
        return self.email
    # 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 + \
               "]"

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()
the new issues look like so

Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
runfile('C:/_safings_/_dev_/python/contacts__100.py', wdir='C:/_safings_/_dev_/python')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.2.4\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.2.4\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/_safings_/_dev_/python/contacts__100.py", line 43
    def getName(self):
    ^
IndentationError: unexpected indent
i will have a closer look
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
RE: rework of a little python-script - extending getters and setters - by apollo - Oct-31-2018, 11:50 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Extending list doesn't work as expected mmhmjanssen 2 237 May-09-2024, 05:39 PM
Last Post: Pedroski55
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,472 Jun-29-2023, 11:57 AM
Last Post: gologica
  Can property getters and setters have additional arguments? pjfarley3 2 3,099 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 6,027 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,365 May-28-2020, 05:27 PM
Last Post: micseydel
  Package python script which has different libraries as a single executable or script tej7gandhi 1 2,669 May-11-2019, 08:12 PM
Last Post: keames
  Extending my text file word count ranker and calculator Drone4four 8 5,440 Jan-25-2019, 08:25 AM
Last Post: steve_shambles
  How to run python script which has dependent python script in another folder? PrateekG 1 3,208 May-23-2018, 04:50 PM
Last Post: snippsat
  How to call one python script and use its output in another python script lravikumarvsp 3 32,515 May-16-2018, 02:08 AM
Last Post: lravikumarvsp
  Check Python version from inside script? Run Pythons script in v2 compatibility mode? pstein 2 9,896 Jul-07-2017, 08:59 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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