I'm trying to create an employee
object via user input, but I'm running into issues with my code. When I run this nothing happens and it's not throwing any errors either.
class employee(object):
def __init__(self,name,pay_rate,monday,tuesday,wednesday,thursday,friday,saturday,sunday):
self.create_employee()
self.name = name
self.pay_rate = pay_rate
self.monday = monday
self.tuesday = tuesday
self.wednesday = wednesday
self.thursday = thursday
self.friday = friday
self.saturday = saturday
self.sunday = sunday
def weekly_total(self):
self.total_weekly_hours = self.monday + self.tuesday + self.wednesday + self.thursday + self.friday + self.saturday + self.sunday
self.emp_name()
print "\n Your hours this week are:", self.total_weekly_hours,"\n"
def emp_name(self):
print "\n Current employee is: ",self.name
def create_employee(self):
self.name = raw_input("Enter new employee name")
self.pay = input("Enter pay rate")
self.monday = raw_input("Enter monday hours")
self.tuesday = raw_input("tuesday hours?")
self.wednesday = raw_input("wed hours?")
self.thursday = raw_input("Thursday hours?")
self.friday = raw_input("Friday hours?")
self.saturday = raw_input("saturday hours?")
self.sunday = raw_input("sunday hours?")
self.object_name = raw_input("Name your object")
self.object_name = employee(self.name,self.pay,self.monday,self.tuesday,self.wednesday,self.thursday,self.friday,self.saturday,self.sunday)
print self.name, " was created"
Your current code has a neverending loop in it, as __init__
and create_employee
call each other. You take arguments for all the attributes in the initialiser, then ignore them and ask for user input, which you pass to the initialiser for a new object, which ignores it and...
I think what you want is a structure more like:
class Employee(object): # PEP-8 name
def __init__(self, name, pay, hours):
# assign instance attributes (don't call from_input!)
def __str__(self):
# replaces emp_name, returns a string
@property
def weekly_total(self):
return sum(self.hours)
@classmethod
def from_input(cls):
# take (and validate and convert!) input
return cls(name, pay, hours)
Which you can use like:
employee = Employee("John Smith", 12.34, (8, 8, 8, 8, 8, 0, 0))
print str(employee) # call __str__
Or:
employee = Employee.from_input()
print employee.weekly_total # access property
Note that rather than having separate instance attributes for the different days, I've assumed a single list/tuple of hours for each day. If the day names are important, use a dictionary {'Monday': 7, ...}
. Remember that all raw_input
is a string, but you probably want hours and pay as floats; for more on input validation, see here.
The reason this isn't running is because you've defined a class, but then there is nothing that is instantiating that class, so nothing happens when you run it.
However, if you were to add that code to instantiate a class, say:
e = employee()
e.create_employee()
you would run into errors caused by the circular nature of the code, as mentioned by @jonrsharpe.
You could move create_employees
outside of the class and have it be a wrapper which takes in input (into regular variables, or a dict
, but not the class itself), and instantiates the object using that input. You could then call that function from the main part of the script.
I would recommend reading through the Python doc on classes so you can become more familiar with the OO paradigm:
https://docs.python.org/2/tutorial/classes.html
I think you want something like this
class employee(object):
def __init__(self,name,pay_rate,monday,tuesday,wednesday,thursday,friday,saturday,sunday):
self.name = name
self.pay_rate = pay_rate
self.monday = monday
self.tuesday = tuesday
self.wednesday = wednesday
self.thursday = thursday
self.friday = friday
self.saturday = saturday
self.sunday = sunday
@staticmethod
def create_from_rawinput():
return employee(
raw_input("Employee name:"),
raw_input("Pay Rate:"),
raw_input("Enter monday hours:"),
raw_input("Enter tuesday hours:"),
raw_input("Enter wednesday hours:"),
raw_input("Enter thursday hours:"),
raw_input("Enter friday hours:"),
raw_input("Enter saturday hours:"),
raw_input("Enter sunday hours:")
)
new_emp = employee.create_from_rawinput()