Reputation: 55
I'm fairly new to python and was having trouble with an inheritance classes. This is for an online introduction to computer programming class and I do not have the resources to figure out where I went wrong with my code. Here is the code:
class Employee:
def __init__(self, employeeName, employeeShift):
self.__name = employeeName
self.__number = employeeShift
def set_name(self, nam):
self.__name = nam
def set__number(self, num):
self.__number = num
# Accessor methods
def get_name(self):
return self.__name
def get_number(self):
return self.__number
# ProductionWorker class that inherited the Employee class
class ProductionWorker(Employee):
def __init__(self, shift, payRate):
self.__shift_number = shift
self.__pay_rate = payRate
def set_shift_number(self, shift):
self.__shift_number = shift
def set_pay_rate(self, pay):
self.__pay_rate = pay
# Accessor methods
def get_shift(self):
return self.__shift
def get_shift(self):
if self.__shift_number = '1':
s = 'Day shift'
else self.__shift_number = '2':
s = 'Night shift'
return s
def get_pay(self):
return self.__pay
def get_pay(self):
if self.__pay_rate <= 5:
p = 7.50
elif self.__pay_rate > 5 and self.__pay_rate <= 15:
p = 13.50
else self.__pay_rate >= 20:
p = 20
return p
# main module
def main():
# Prompts the user for employee name, shift and pay rate
employeeName = raw_input("Please enter the employee name: ")
print "Please enter which shift the employee works."
employeeShift = input("1 for day shift, 2 for night shift: ")
payRate = input("How long has the employee worked here? ")
# Creates object of ProductionWorker
worker = ProductionWorker(employeeName, employeeShift, payRate)
# Displays information
print "Employee name:", worker.get_name()
print "Employee shift:", worker.get_shift()
print "Employee pay rate:", worker.get_pay_rate()
# calls main module
main()
It tells me that there is a syntax error in the ProductionWorker
class, under the def get_shift()
where I have the self.__shift_number = '1'
. I followed my professor's example, so I don't really understand this error.
Upvotes: 0
Views: 252
Reputation: 9402
You should be using '==' instead of '=' in the get_shift function.
= is the assignment operator
== is the comparison operator
Corrected Code:
def get_shift(self):
if self.__shift_number == '1':
s = 'Day shift'
elif self.__shift_number == '2':
s = 'Night shift'
return s
Upvotes: 1
Reputation:
Python uses ==
for comparison tests:
if self.__shift_number == '1':
=
if for variable assignment.
Edit:
You actually have a few more problems. Before I get into them, here is a fixed version of your script:
class Employee:
def __init__(self, employeeName, employeeShift):
self.name = employeeName
self.number = employeeShift
def set_name(self, nam):
self.name = nam
def setnumber(self, num):
self.number = num
# Accessor methods
def get_name(self):
return self.name
def get_number(self):
return self.number
# ProductionWorker class that inherited the Employee class
class ProductionWorker(Employee):
##############################################
def __init__(self, name, shift, payRate):
self.name = name
##############################################
self.shift_number = shift
self.pay_rate = payRate
def set_shift_number(self, shift):
self.shift_number = shift
def set_pay_rate(self, pay):
self.pay_rate = pay
# Accessor methods
def get_shift(self):
if self.shift_number == 1:
s = 'Day shift'
###################################
elif self.shift_number == 2:
###################################
s = 'Night shift'
return s
def get_pay(self):
if self.pay_rate <= 5:
p = 7.50
elif self.pay_rate > 5 and self.pay_rate <= 15:
p = 13.50
###################################
elif self.pay_rate >= 20:
###################################
p = 20
return p
# main module
def main():
# Prompts the user for employee name, shift and pay rate
employeeName = raw_input("Please enter the employee name: ")
print "Please enter which shift the employee works."
employeeShift = input("1 for day shift, 2 for night shift: ")
payRate = input("How long has the employee worked here? ")
# Creates object of ProductionWorker
worker = ProductionWorker(employeeName, employeeShift, payRate)
# Displays information
print "Employee name:", worker.get_name()
print "Employee shift:", worker.get_shift()
print "Employee pay rate:", worker.get_pay()
# calls main module
main()
Now here is what I did:
Replaced all of your else
's with elif
's (since else
does not support a condition).
Except the ones in __init__
, I removed every double underscore. This is because double underscores, when used like that, have a special meaning in Python.
Added a name
parameter to ProductionWorker.__init__
(since you pass a name in on this line: worker = ProductionWorker(employeeName, employeeShift, payRate)
).
Just below the declaration of ProductionWorker.__init__
, I added this line to give ProductionWorker
a name attribute: self.name = name
.
Upvotes: 4
Reputation: 25964
In addition to the bug pointed out by @iCodez, you haven't defined what self.__shift
is, so get_shift()
will raise an error when you call it.
You also haven't defined what self.__pay
is, so get_pay()
will throw a NameError
.
I assume you meant those methods to return self.__shift_number
and self.pay_rate
, respectively.
Haha, actually now that I look closer, you double-define those methods, so you immediately overwrite your buggy code with more-correct code. I suggest you delete your buggy code.
Upvotes: 2