python - How do I take the users input and if it equals 1 it would be day and a 2 would be night? -
i trying user's input , if enter 1 shift print out day shift , 2 equal night shift. doing using object oriented not know how new it. also, have read on here way taught set classes not best way python how professor wants not can that.
class employee
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
class 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 users name: ") number = input("enter users number: ") shift = input("enter users shift (1, 2): ") pay = input("enter users hourly pay rate: ") employee = productionworker.productionworker(name, number, shift, pay) empshift = employee.get_shift() print(employee) main()
i make method called display_shift
shows human-readable shift name stored in dict
property of static class:
class productionworker(employee.employee): shift_names = { '1': 'day', '2': 'night', 'w': 'weekend', # example how 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'
alternatives static dict shift_names
are:
an instance dict set inside
__init__
self.shift_names = {...}
, allow methoddef add_shift(self, shift_number, shift_name)
add new shifts company may need (overtime? weekend? holiday?)a tuple
shift_names = ('day', 'night')
,display_shift
return self.shift_names[int(self.get_shift()) - 1]
. benefit of inability change or add shift names. drawback shifts need remain consecutively orderedint
s , direct relation between shift number , shift name isn't explicitly clear.a separate static class or
collections.namedtuple
other possibilities
edit:
to show shift when str(employee)
called use __str__
magic method this:
def __str__(self): return '{}({}, #{}, {} shift, ${}/hr)'.format( self.__class__.__name__, self.get_name(), self.get_number(), self.display_shift(), self.get_pay())
this result in following:
>>> print(employee) productionworker(john doe, #12345, day shift, $40/hr)
Comments
Post a Comment