-1

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"
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Achilles
  • 195
  • 3
  • 12
  • That code makes absolutely no sense. `__init__` calls `create_employee`, which calls `__init__`, which... And what does *"name your object"* mean? – jonrsharpe Jul 27 '14 at 22:03
  • name your object means , name your object not quite sure how else to explain that to you. – Achilles Jul 27 '14 at 22:34
  • Could you give an example of what you expect it to do? How is it different to `name`? Are you trying to allow the user to input e.g. the `'emp1'` in `emp1 = employee(...)`? If so, that is **not** how Python works. – jonrsharpe Jul 27 '14 at 22:37
  • name is the employee name as a string as an attribute of the object, and object name is the name of the employee object itself. – Achilles Jul 27 '14 at 22:50
  • Then I think you have misunderstood how names in Python work. See e.g. [this](http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables). You should keep data out of your variable names; have a list or dictionary of the `Employee` objects instead. – jonrsharpe Jul 27 '14 at 22:52

3 Answers3

1

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

khampson
  • 14,700
  • 4
  • 41
  • 43
1

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.

Community
  • 1
  • 1
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • I kind of sort of get what your saying but not all the way and the code you wrote is confusing to me, sort of cryptic. – Achilles Jul 27 '14 at 22:38
  • @Achilles could you expand on *"sort of cryptic"*? Are there specific parts you don't (or do) understand? This is just the outline; you need to fill in the actual code yourself. – jonrsharpe Jul 27 '14 at 22:39
  • Yes, i intend on filling it in myself but i don't get exactly what your saying. Hours is a dictionary? WHere it says @classmethod what do you mean by that? The from_input function would i just use my same input statements i already have coded? Where you have employee = Employee("John Smith", 12.34, (8, 8, 8, 8, 7, 0, 0)) i want to do this automatically from input of the user and store it. – Achilles Jul 27 '14 at 22:55
  • I do apologize im rather new to programming this is my first language. – Achilles Jul 27 '14 at 22:56
  • @Achilles yes, `hours` could be a dictionary, depending on what you need; it's a tuple in my example. Search for "python class method" and find e.g. [this](http://stackoverflow.com/a/12179752/3001761). No, follow [the link I provided](http://stackoverflow.com/a/23294659/3001761) and write new input functions with validation. Yes, that's what the second example `employee = Employee.from_input()` does. If you're new, read [the docs](https://docs.python.org/2/tutorial/classes.html). – jonrsharpe Jul 27 '14 at 22:58
  • Thanks i have read a lot of the docs but its still confusing even after you read it so I've been trying to pick things up by writing code instead. – Achilles Jul 27 '14 at 23:08
0

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()
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179