-6

I am trying to create a Python script (WageEarner) that can import my PersonWorker class. I need it to prompt for a person’s first name, last name, and phone number because it will create a PersonWorker object using the information provided by the user. I need it to prompt the user for the hours worked for the week and the pay rate. It will print out the PersonWorker object and weekly pay by calling the getWeeksPay method for that object. I am new to Python so I am not familiar on how to do this.

Here is my PersonWorker class:

class PersonWorker:

    def _init_(self, firstName, lastName, phoneNo, rate=0):
        self.firstName= firstName
        self.lastName= lastName
        self.phoneNo= phoneNo
        self.rate= rate
    def getFirstName(self):
        return self.firstName
    def getLastName(self):
        return self.lastName
    def getPhoneNo(self):
        return self.phoneNo
    def getWeeksPay(self,hours):
        if rate is 0: raise Exception("Rate not set")
        return hours*self.rate
    def _str_(self): 
        stringRep = "First Name: " + self.firstName + "\n"
        stringRep = "Last Name: " + self.lastName + "\n"
        stringRep = "Phone Number : " + self.phoneNo + "\n"
        return stringRep
Sabuncu
  • 5,095
  • 5
  • 55
  • 89
  • 5
    First, what have you written so far? What part are you stuck on? Second, `_init_` and `_str_` won't work; you need `__init__` and `__str__` (double underscores on each end). This, is this Python 2, or 3? If it's 2, you should write `class PersonWorker(object):` for the first line. And finally, you're replacing `stringRep` repeatedly, instead of adding to it; you probably wanted `+=` instead of `=` for the second and third lines. – abarnert Aug 02 '13 at 21:50
  • 3
    @abarnert I'm assuming they're working on the same assignment as http://stackoverflow.com/questions/18026306/classes-within-python-part-1 – Jon Clements Aug 02 '13 at 21:51
  • @JonClements I thought I was having Deja vu... – jh314 Aug 02 '13 at 21:52
  • @JonClements: Based on their using the same gravatar, I guess they are the same person :) – Tim Pietzcker Aug 02 '13 at 21:52
  • @JonClements: Wow, they even made the same mistakes with `_init_` and `_str_`, and with repeatedly overwriting `stringRep`. Did they professor actual give them broken code to try to force them to debug it or something? – abarnert Aug 02 '13 at 21:52
  • `if rate is 0:` needs to be `if self.rate == 0:`, as well. – Tim Pietzcker Aug 02 '13 at 21:53
  • We can't close this as a dup to a still-open question, so… what are we supposed to do instead? There's obviously no point answering the exact same question twice, whether it's the same person or two classmates. – abarnert Aug 02 '13 at 21:54
  • Using getter methods for basic attribute access is also not very Pythonic. Just use `person.firstName` rather than `person.getFirstName()`! – Blckknght Aug 02 '13 at 21:55
  • 10
    tell whatever teacher gave you this assignment to look up python **properties**, this getter/setter business ain't gonna fly here. – Ryan Haining Aug 02 '13 at 21:55
  • @abarnert well - let's hope people heed the comments that it's a dupe, and I've flagged the post... we'll see what happens I guess – Jon Clements Aug 02 '13 at 21:57
  • 1
    Do you mean you're trying to *inherit* from this class? – Ryan Haining Aug 02 '13 at 21:57
  • 4
    @RyanHaining: +1… Or, better, find a teacher who actually knows Python, or a book or a tutorial. The OP can still show up to class to turn in assignments; it should be pretty easy to get an A when competing with people who are too busy learning how to write 30 lines of Java-style boilerplate for every line of Python to get any of the actual code correct… – abarnert Aug 02 '13 at 21:59
  • we are trying to figure out how to solve this problem. We are hoping for help not answers. but none of us understand how to go about this problem. – user2647434 Aug 02 '13 at 22:00
  • 2
    What problem? You didn't ask any question. – Lennart Regebro Aug 02 '13 at 22:06
  • Learn how to do all this in a sequential python script, then make functions, then switch to OOP and reuse these bits of codes in the class. – DevLounge Aug 02 '13 at 22:08
  • why is this tagged as `python` and not `java`? – Ryan Haining Aug 02 '13 at 22:57
  • @Apero then make it purely functional – Ryan Haining Aug 03 '13 at 00:37

1 Answers1

3
'''
This code released under the ijustmadethisup license
You are free to redistribute, modify, reuse, whatever, for any purpose
that does not violate the following conditions:

    1) You may not submit this as your own work for a homework assignment
    2) You must include this license in any distribution of the code

thanks for reading

'''

class PersonWorker(object):
    def __init__(self, first_name, last_name, phone_number, rate=0):
        self._first_name= first_name
        self._last_name= last_name
        self._phone_number= phone_number
        self._rate= rate

    @property
    def first_name(self):
        return self._first_name

    @property
    def last_name(self):
        return self._last_name

    @property
    def phone_number(self):
        return self._phone_number

    def weeks_pay(self, hours):
        if self._rate == 0:
            raise ValueError('Rate not set')

        return hours*self._rate

    def __str__(self): 
        return 'First Name: {0}\nLast Name: {1}\nPhone Number: {2}'.format(
            self.first_name, self.last_name, self.phone_number)

def main():
    first_name = raw_input('Enter first name: ')
    last_name = raw_input('Enter last name: ')
    phone_number = raw_input('Enter phone number: ')
    rate = float(raw_input('Enter rate: '))
    pw = PersonWorker(first_name, last_name, phone_number, rate)

    hours = float(raw_input('Enter hours worked: '))
    print pw
    print 'Pay:', pw.weeks_pay(hours)

if __name__ == '__main__':
    main()
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174