-6

我正在尝试创建一个可以导入我的PersonWorker课程的 Python 脚本(WageEarner)。我需要它来提示一个人的名字、姓氏和电话号码,因为它将PersonWorker使用用户提供的信息创建一个对象。我需要它来提示用户一周的工作时间和工资率。它将通过调用该对象的方法打印出PersonWorker对象和每周工资。getWeeksPay我是 Python 新手,所以我不熟悉如何做到这一点。

这是我的PersonWorker课:

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
4

1 回答 1

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()
于 2013-08-02T22:51:53.340 回答