我如何接受用户输入,如果它等于1则为白天,2为夜晚?

时间:2015-06-18 22:10:01

标签: python

我正在尝试获取用户的输入,如果输入1表示班次,它将打印出白班,2则等于夜班。我使用面向对象这样做,所以我不知道如何做到这一点,因为我是新手。另外,我在这里读到,我被教导设置我的课程的方式不是为python做这个的最佳方式,但这就是教授想要的方式,所以我无能为力。

班级员工

class Employee:
    def __init__(self, name, number):
        self.__name = name
        self.__number = number

    def set_name(self, name):
        self.__name = name

    def set_number(self, number):
        self.__number = number

    def get_name(self):
        return self.__name

    def get_number(self):
        return self.__number

类ProductionWorker

import employee

class ProductionWorker(employee.Employee):
    def __init__(self, name, number, shift, pay):
        employee.Employee.__init__(self, name, number)

        self.__shift = shift

        self.__pay = pay

    def set_shift(self, shift):
        self.__shift = shift


    def set_pay(self, pay):
        self.__pay = pay

    def get_shift(self):
        return self.__shift

    def get_pay(self):
        return self.__pay

    def dayNight(self, shift):
        if shift == 1:
            self.__shift = "Day Shift"
        else:
            self.__shift = "Night Shift"

    def __str__(self):
        return "\nName: " + self.get_name() + "\nNumber: " + self.get_number() + \
               "\nShift: " + self.get_shift() + "\nPay: " + self.__pay

employeeTest

import ProductionWorker


def main():
    name = input("Enter the users name: ")
    number = input("Enter the users number: ")
    shift = input("Enter the users shift (1, 2): ")
    pay = input("Enter the users hourly pay rate: ")    


    employee = ProductionWorker.ProductionWorker(name, number, shift, pay)
    empShift = employee.get_shift()
    print(employee)

main()

3 个答案:

答案 0 :(得分:0)

您可以使用dict映射转换为" 1"和" 2"然后使用实例移位值进行查找。

class ProductionWorker(employee.Employee):
    def __init__(self, name, number, shift, pay):
        employee.Employee.__init__(self, name, number)
        self.shifts = {"1":"Day shift","2":"Night shift"}  


def main():
    name = input("Enter the users name: ")
    number = input("Enter the users number: ")
    shift = input("Enter the users shift (1, 2): ")
    pay = input("Enter the users hourly pay rate: ")    


    employee = ProductionWorker.ProductionWorker(name, number, shift, pay)
    empShift = employee.get_shift()
    print(employee)
    print(employee.shifts[empShift])

main()

如果你的班级需要,你可以使用一种方法,逻辑基本相同。

要使用__str__打印所有详细信息,只需使用get_shift方法进行查找,而不是仅返回自我.__ shift:

class ProductionWorker.ProductionWorker(Employee):
    def __init__(self, name, number, shift, pay):
         employee.Employee.__init__(self, name, number)
        self.shifts = {"1": "Day shift", "2": "Night shift"}
        self.__shift = shift
        self.__pay = pay

    def set_shift(self, shift):
        self.__shift = shift

    def set_pay(self, pay):
        self.__pay = pay

    def get_shift(self):
        return self.shifts[self.__shift]

    def get_pay(self):
        return self.__pay

    def dayNight(self, shift):
        return self.shifts[shift]

    def __str__(self):
        return "\nName: {}\nNumber: {}\nShift: {}\nPay: {}".\
            format(self.get_name(), self.get_number(), self.dayNight(self.get_shift()), self.__pay)

输出:

Enter the users name: foo
Enter the users number: 1234
Enter the users shift (1, 2): 2
Enter the users hourly pay rate: 100

Name: foo
Number: 1234
Shift: Night shift
Pay: 100

如果你想将你的DayNight方法与if / else一起使用,你必须比较一个字符串而不是一个int,最简单的方法就是返回字符串:

    def set_shift(self, shift):
        self.__shift = shift

    def set_pay(self, pay):
        self.__pay = pay

    def get_shift(self):
        return self.__shift

    def get_pay(self):
        return self.__pay

    def dayNight(self, shift):
        if shift == "1":
            return"Day Shift"
        else:
            return "Night Shift"

    def __str__(self):
        return "\nName: {}\nNumber: {}\nShift: {}\nPay: {}".\
            format(self.get_name(), self.get_number(), self.dayNight(self.get_shift()), self.__pay)

答案 1 :(得分:0)

我会创建一个名为display_shift的方法,它显示存储在静态类的dict属性中的人类可读移位名称:

class ProductionWorker(employee.Employee):
    SHIFT_NAMES = {
        '1': 'day',
        '2': 'night',
        'W': 'weekend',  # just an example how you could easily add other shifts
    }

    # ...other code...

    def display_shift(self):
        return self.SHIFT_NAMES[self.get_shift()]

# ...init code...

employee = ProductionWorker(name, number, shift, pay)
print(employee.display_shift())  # 'day' or 'night'

静态字典SHIFT_NAMES的替代方法是:

  • __init__内设置self.shift_names = {...}的实例字典,这会允许像def add_shift(self, shift_number, shift_name)这样的方法添加公司可能需要的任何新班次(加班?周末?度假?)

  • SHIFT_NAMES = ('day', 'night')display_shift之类的元组,如return self.SHIFT_NAMES[int(self.get_shift()) - 1]。这样做的好处是无法更改或添加班次名称。缺点是需要保持连续排序int s的变化,并且变速号和班次名称之间的直接关系不明确。

  • 单独的静态类或collections.namedtuple是其他可能性

编辑:

要显示调用str(employee)时的转变,您可以使用__str__魔术方法执行以下操作:

def __str__(self):
    return '{}({}, #{}, {} shift, ${}/hr)'.format(
        self.__class__.__name__, self.get_name(), self.get_number(),
        self.display_shift(), self.get_pay())

这将导致类似以下内容:

>>> print(employee)
ProductionWorker(John Doe, #12345, day shift, $40/hr)

你可以read more about Python magic methods here.

答案 2 :(得分:0)

问题是你已经在类ProductionWorker中定义了一个实例方法dayNight但从未使用过它,你应该修改difList并在实例化类对象之后显式调用它(也不需要传递shift参数) :

dayNight定义:

    def dayNight(self):
        if self.__shift == 1:
            self.__shift = "Day Shift"
        else:
            self.__shift = "Night Shift"

然后像这样实例化:

    employee = ProductionWorker.ProductionWorker(name, number, shift, pay)
    employee.dayNight()
    print(employee)

OR

仅通过修改 init 来执行此操作:

    def __init__(self, name, number, shift, pay):
        employee.Employee.__init__(self, name, number)
        if shift == 1:
            self.__shift = "Day Shift"
        else:
            self.__shift = "Night Shift"

然后像这样实例化:

    employee = ProductionWorker.ProductionWorker(name, number, shift, pay)
    print(employee)