如果= 1则答案是白班,否则答案是夜班

时间:2014-07-10 18:00:39

标签: python

使用此程序,结果应显示员工姓名,号码,班次(基于推算数字的白天或夜晚)和小时工资率。

我尝试了几种不同的方式,可以“几乎”获得理想的效果。任何帮助将不胜感激。

# This Employee class holds general data about employess and will
# end up as the superclass for this example.
class Employee:
    #__init__ method initialzes the attributes.
    def __init__(self, emp_name, emp_number):
        self.__emp_name = emp_name
        self.__emp_number = emp_number

    # The set_emp_name method gets the employee name.
    def set_emp_name(self, emp_name):
        self.__emp_name = emp_name

    # The set_emp_name method gets the employee number.
    def set_emp_number(self, emp_number):
        self.__emp_number = emp_number

    # The get_emp_name method returns the employee name.
    def get_emp_name(self):
        return self.__emp_name

    # The get_emp_number method returns the employee number.
    def get_emp_number(self):
        return self.__emp_number



# The ProductionWorker class holds the general data from superclass Employee
# as well as Employee shift time and pay rate making it a subclass
# of Employee.
class ProductionWorker(Employee):
    # __init__ method initializes the attributes.
    def __init__(self, emp_name, emp_number, shift, payrate):
        # Call the superclass
        Employee.__init__(self, emp_name, emp_number)
        self.__shift = shift
        self.__payrate = payrate

        # The set_shift method get employee shift.
    def set_shift(self, shift):
        self.__shift = shift

        # The set_payrate method gets employee hourly pay rate.
    def set_payrate(self, payrate):
        self.__payrate = payrate

        # The get_shift method returns the employee shift.
    def get_shift(self):
        if self.shift == 1:
           self.shift = 'Day shift'
        elif self.shift == 2:
            self.shift = 'Night shift'
        return self.__shift

        # The get_payrate method returns the employee hourly pay rate.
    def get_payrate(self):
        return self.__payrate

# This program will test the Employee superclass and ProductionWorker subclass 
# by returning and displaying the gathered information.

import sys

# Get the Employee info.
emp_name = input('Employee Name: ')
emp_number = input('Employee Number: ')
shift = float(input ('Shift Number 1 or 2: '))
payrate = input('Hourly Pay Rate: $')


# Determine True or False for mailing list.
#if shift == 1:
    #print('Day')
#else:
    #print ('Night')

# Create an instance of the ProductionWorker class.
my_productionworker = ProductionWorker(emp_name, emp_number, shift, payrate)

# Display the object's data.
print('Employee Information')
print('---------------------')
print('Employee Name:', my_productionworker.get_emp_name())
print('Employee Number:', my_productionworker.get_emp_number())
print('Shift:', my_productionworker.get_shift())
print('Hourly Pay Rate:$', my_productionworker.get_payrate())

1 个答案:

答案 0 :(得分:3)

在你的方法中

def get_shift(self):
    if self.shift == 1:
       self.shift = 'Day shift'
    elif self.shift == 2:
        self.shift = 'Night shift'
    return self.__shift

你混淆了.shift.__shiftifelif中,它应该是.__shift,在分配中它应该只是shift {1}}(一个局部变量,而不是一个成员变量)然后你应该return那个局部变量(或者直接从if和elif内部返回,具体取决于你对多个出口点的感觉)。

此外,最好在else中加入if / elif,这可能是这样的:

 else:
    shift = 'Cannot convert {} to a valid shift.'.format(self.__shift)

 else:
    raise Exception('Invalid shift value {}.'.format(self.__shift)

会提醒您注意所有有效选项,并提供一个关于导致问题的价值的提示。

顺便说一下,你不应该以这种方式使用双下划线变量,一般来说,不应该尝试用Python编写Java代码。我将摆脱getter和setter,直接读取和写入成员值。