-6

クラスをインポートできる Python スクリプト (WageEarner) を作成しようとしていますPersonWorkerPersonWorkerユーザーから提供された情報を使用してオブジェクトを作成するため、人の名、姓、電話番号の入力を求めるプロンプトが必要です。1 週間の労働時間と賃金率をユーザーに確認する必要があります。オブジェクトのメソッドを呼び出して、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 に答える