14: This Looks Like a Job For... (error present in my code)


#1

hello everyone,

this is the task:

First, inside your PartTimeEmployee class:

Add a new method called full_time_wage with the arguments self and hours.
That method should return the result of a super call to the calculate_wage method of PartTimeEmployee’s parent class. Use the example above for help.
Then, after your class:

Create an instance of the PartTimeEmployee class called milton. Don’t forget to give it a name.
Finally, print out the result of calling his full_time_wage method. You should see his wage printed out at $20.00 per hour! (That is, for 10 hours, the result should be 200.00.)

so this is my code


class Employee(object):
  """Models real-life employees!"""
  def __init__(self, employee_name):
    self.employee_name = employee_name

  def calculate_wage(self, hours):
    self.hours = hours
    return hours * 20.00

# Add your code below!
class PartTimeEmployee(Employee):
  def calculate_wage(self, hours):
    self.hours = hours
    return hours * 12.00
  def full_time_wage(self, hours):
    return super(PartTimeEmployee, self).calculate_wage(self, hours)
  
Milton = PartTimeEmployee("Milton")
print full_time_wage(Milton, 10)


actually I check the Hint and I see this:

You super call should look something like this:

def full_time_wage(self, hours):
return super(PartTimeEmployee, self).method(args)
Where method is the method you want (calculate_wage) and args are the arguments that method takes.

so I supposed that the first part of the excersise I did correctly:

but then I get this error:

Traceback (most recent call last):
File “python”, line 19, in
NameError: name ‘full_time_wage’ is not defined
python:1: SyntaxWarning: import * only allowed at module level

So I suppose that the error is actually in this part:

Milton = PartTimeEmployee(“Milton”)
print full_time_wage(Milton, 10)

but I don’t understand my error

thank you very much for your help


#2

Hi @asernot,

It would be best to begin the variable name with a lowercase letter here …

Milton = PartTimeEmployee("Milton")

Name the variable milton, instead.

Here, you are attempting to call full_time_wage as if it is a global function, when it is actually a method of the PartTimeEmployee class …

print full_time_wage(Milton, 10)

Remove Milton as an argument, and use dot notation to call the method from the variable that refers to the appropriate instance, which should be milton.


#3

Hi @appylpye

thank you for your suggestions:

I modified my code in this way:

class Employee(object):
  """Models real-life employees!"""
  def __init__(self, employee_name):
    self.employee_name = employee_name

  def calculate_wage(self, hours):
    self.hours = hours
    return hours * 20.00

# Add your code below!
class PartTimeEmployee(Employee):
  def calculate_wage(self, hours):
    self.hours = hours
    return hours * 12.00
  def full_time_wage(self, hours):
    return super(PartTimeEmployee, self).calculate_wage(self, hours)
  
milton = PartTimeEmployee("milton")
print milton.full_time_wage(10)

and there is this error message:

Traceback (most recent call last):
File “python”, line 19, in
File “python”, line 16, in full_time_wage
TypeError: calculate_wage() takes exactly 2 arguments (3 given)
python:1: SyntaxWarning: import * only allowed at module level


#4

The problem is in this statement …

        return super(PartTimeEmployee, self).calculate_wage(self, hours)

Within that line of code, super(PartTimeEmployee, self) is an object that represents milton, which is a PartTimeEmployee instance. That object enables milton to behave, instead, as a Employee instance. This is so that you can call the calculate_wage method of the Employee class instead of that of the PartTimeEmployee class. It is from that object that you are calling the calculate_wage method. However, note that when you call that method, you should pass it only one argument, representing hours. Therefore, you need to remove the self argument from within the parentheses for that method call.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.