0

I am writing a code to document an employee system, the class can print full name, email:

class Employee:

    def __init__(self,first,last):
        self.first=first
        self.last=last
        
    def fullname(self):
        print('{} {}'.format(self.first,self.last))
    def email(self):
        print('{}.{}@email.com'.format(self.first,self.last))

emp_1=Employee('John','Smith')
emp_1.first='Jim'

print(emp_1.first)
print(emp_1.email())
print(emp_1.fullname())

Output is like this:

output

I don't understand why when I call methods (email(), fullname()), I have a None within the output?

The output is:

Jim
[email protected]
None
Jim
Smith
None
3
  • 4
    i didn't look at the screenshot. you need to return something from those methods to see it in the output. Commented Mar 4, 2022 at 2:58
  • You're confusing what's being printed with what data is returned from each function. print is just for you to visualize what a value is. It has nothing to do with how data is transferred or is flowing through the computer. Commented Mar 4, 2022 at 11:28
  • When you don't return anything from a function/method the standard return value is None. So you have 2 choices: (1) Return the strings without printing and then print the returns. (2) Print the strings in the methods but don't print the returns. Commented Mar 4, 2022 at 11:29

1 Answer 1

1

You are using the method call inside a print function. So it will try to print the value which is returned from the method. Since you are not returning anything from the method, it prints None.

You can always return the value instead of printing them inside. Example.

class Employee:

    def __init__(self, first, last):
        self.first = first
        self.last = last

    def fullname(self):
        # just printing the name will not return the value.
        return '{} {}'.format(self.first, self.last)

    def email(self):
        # same, use a return statement to return the value.
        return '{}.{}@email.com'.format(self.first, self.last)


emp_1 = Employee('John', 'Smith')
emp_1.first = 'Jim'

print(emp_1.first)
print(emp_1.email())  # print what is returned by the method.
print(emp_1.fullname())

It will give a proper output like

Jim
[email protected]
Jim Smith
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.