3ncrypt3d
3ncrypt3d

Reputation: 29

Type Error: A float is required

Here's the error that I'm getting:

Running...done.
Traceback (most recent call last):
  File "main.py", line 13, in <module>
    print('Alice:\n Net pay: $%7.2f' % alice.calculate_pay())
TypeError: a float is required

Weird error, that I have no idea on what to do. I'm obviously beginning with Python, and thus far bug tracking has been easy, minus this. Here's the code below:

class Employee:
   def __init__(self):
       self.wage = 0
       self.hours_worked = 0

   def calculate_pay(self):
      self.calculate_pay = (self.wage * self.hours_worked)


alice = Employee()
alice.wage = 9.25
alice.hours_worked = 35
print('Alice:\n Net pay: $%7.2f' % alice.calculate_pay())

bob = Employee()
bob.wage = 11.50
bob.hours_worked = 20
print('Bob:\n Net pay: $%7.2f' % bob.calculate_pay())

Upvotes: 0

Views: 305

Answers (4)

Marcin
Marcin

Reputation: 238081

Change the method and instance variable to different names, and make return statment in your calculate_pay method:

def calculate_pay(self):
    self.calculated_pay = (self.wage * self.hours_worked)
    return  self.calculated_pay

Upvotes: 0

Neel
Neel

Reputation: 21243

You are calling calculate_pay method and its not returning anything, means it return None by default.

Please return value from function and use it or use variable instead or method

Variable Use

alice.calculate_pay()
print('Alice:\n Net pay: $%7.2f' % alice.calculate_pay)

OR

Return from function

def calculate_pay(self):
    return (self.wage * self.hours_worked)

Upvotes: 2

hjpotter92
hjpotter92

Reputation: 80629

You're missing the return statement:

def calculate_pay(self):
  self.calculate_pay = (self.wage * self.hours_worked)
  return self.calculate_pay

Upvotes: 4

sbrichards
sbrichards

Reputation: 2210

Change

def calculate_pay(self):
    self.calculate_pay = (self.wage * self.hours_worked)

to

def calculate_pay(self):
      self.calculate_pay = (self.wage * self.hours_worked)
      return self.calculate_pay

The initial one is not callable and equals NoneType. Even though the value stored to self.calculate_pay is float you cannot reference it like that.

Upvotes: 1

Related Questions