1

I have been messing around with some code, trying to create a function for work planning. However I am stuck and wondered if someone could help? Thanks

class Work_plan(object):
    def __init__(self,hours_work,work_len, work_des):
        self.hours_work = hours_work
        self.work_len = work_len
        self.work_des = work_des

        work_load = []
        hours_worked = []
        if hours_worked > hours_work:
            print "Too much work!"
        else:
            work_load.append(work_des)
            hours_worked.append(work_len)
            print "The work has been added to your work planning!"

work_request = Work_plan(8, 2, "task1")
Work_plan
print work_load

it comes up with the error: NameError: name 'work_load' is not defined

IMSoP
  • 89,526
  • 13
  • 117
  • 169
Mrv
  • 13
  • 2

1 Answers1

0

You defined the variable work_load inside the __init__ of the class, so you can't access it outside this scope.

If you want to have access to work_load, make it an attribute for objects of Work_plan class, and the access it by doing object.work_plan

For example:

class Work_plan(object):
    def __init__(self,hours_work,work_len, work_des):
        self.hours_work = hours_work
        self.work_len = work_len
        self.work_des = work_des

        self.work_load = []
        self.hours_worked = []
        if hours_worked > hours_work:
            print "Too much work!"
        else:
            self.work_load.append(work_des)
            self.hours_worked.append(work_len)
            print "The work has been added to your work planning!"

work_request = Work_plan(8, 2, "task1")
Work_plan
print work_request.work_load
rafaelc
  • 57,686
  • 15
  • 58
  • 82
  • Thank you for replying! When I use your code, it comes up with: Traceback (most recent call last): File "w.p_suggested_answer.py", line 16, in work_request = Work_plan(8, 2, "task1") File "w.p_suggested_answer.py", line 9, in __init__ if hours_worked > hours_work: NameError: global name 'hours_worked' is not defined – Mrv May 27 '15 at 17:49
  • @Mrv use `self.hours_work` instead of `hours_work` every time, and `self.hours_worked` instead of `self.hours_worked` – rafaelc May 27 '15 at 18:25
  • Thank you! I would have given up without your help. – Mrv May 28 '15 at 19:31
  • @Mrv Do not forget to accept the answer as best answer if it helped you :) ! Cheers – rafaelc May 28 '15 at 20:09