Python Forum
Method returning None - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Method returning None (/thread-42071.html)



Method returning None - husksedgier - May-04-2024

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.


RE: Method returning None - Gribouillis - May-04-2024

(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.


RE: Method returning None - husksedgier - May-04-2024

(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!