-2
class A:
    def __init__(self, initialage):
        self.age= initialage
        print(self.age)
        print(initialage)
    def increaseby1(self):
        self.age += 1
        print(self.age)
a=A(5)
print(a.increaseby1())
rayryeng
  • 102,964
  • 22
  • 184
  • 193
S k
  • 23
  • 5
  • tl;dr: `increaseby` does not return anything so by default it returns `None`. You are printing the return value of `increaseby` when you shouldn't have to. Remove the `print` statement for the last line of your code. – rayryeng Jun 10 '19 at 07:39

2 Answers2

0

Your function doesn't have a return value, meaning it implicitly returns None. To avoid that, either don't wrap the function call in a print statement or return the value you intended to print inside the function

Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
0

Because your function doesn't return anything, it just prints age. And since you try to print it's returning value, which is None, it prints None

I suppose you want to print just the self.age, and you want to get rid of printing None. The solution to this problem would be: a.increaseby1() instead of print(a.increaseby1())

So you print the value you want in the function, and then just call it, instead of printing it's returning value, which is None

or

return self.age instead of print(self.age) in your increaseby1 function. This way your function will increment the value and the return it, so when you call your function in print statement it will print self.age

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
VooXe
  • 52
  • 7
  • Except that is the code perfect? Or it can be improvised? – S k Jun 10 '19 at 07:46
  • I suppose you want to print just the `self.age`, and you want to get rid of printing `None`. The solution to this problem would be: `a.increaseby1()` instead of `print(a.increaseby1())` So you print the value you want in the function, and then just call it, instead of printing it's returning value, which is None or `return self.age` instead of `print(self.age)` in your `increaseby1` function. This way your function will increment the value and the return it, so when you call your function in `print` statement it will print `self.age` – VooXe Jun 10 '19 at 07:53
  • `def pal(s): if s==s[::-1]: print('palindrome') else: print('not a palindrome') pal('s')` even though i am not returning anything here, there is no 'none' in the output. can someone explain this? @Bhargav Rao @VooXe – S k Jul 06 '19 at 01:13