Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Method returning None
#1
class Employee:
    no_of_employees = 0
    raise_amount = 1.04

    def __init__(self, first, last, pay):
        self.first = first
        self.last = last
        self.pay = pay
        self.email = first + '.' + last + '@company.com'

    def fullname(self) -> str:
        return '{} {}'.format(self.first, self.last)

    def apply_raise(self):
        self.pay = int(self.pay * self.raise_amount)


Emp1 = Employee('foo', 'bar', 50000)

print(Emp1.apply_raise())
Output:
None
I would like to understand where I am going wrong. I am expecting the output to be 52,000.
Reply
#2
(May-04-2024, 11:12 AM)husksedgier Wrote: I would like to understand where I am going wrong.
All functions without a return statement return None in Python. If you want to return another value, include a return statement.

In your case however, apply_raise() is a "procedure", logically speaking: it does something (apply a raise) but it does not need to return any value. You could perhaps execute the action, then print the employee's pay instead of the result of the function.
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
(May-04-2024, 11:36 AM)Gribouillis Wrote:
(May-04-2024, 11:12 AM)husksedgier Wrote: I would like to understand where I am going wrong.
All functions without a return statement return None in Python. If you want to return another value, include a return statement.

In your case however, apply_raise() is a "procedure", logically speaking: it does something (apply a raise) but it does not need to return any value. You could perhaps execute the action, then print the employee's pay instead of the result of the function.

Thanks a ton for shedding light... I'm still waddling through the Python jungle, barely out of the crib!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class method returning multiple values,dont know which is returned when.. ujjwalrathod007 4 13,054 Oct-03-2016, 08:29 PM
Last Post: ujjwalrathod007

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020