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"
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:
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 adict
, 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
Your current code has a neverending loop in it, as
__init__
andcreate_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:
Which you can use like:
Or:
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 allraw_input
is a string, but you probably want hours and pay as floats; for more on input validation, see here.I think you want something like this