Python Forum

Full Version: How to avoid "None"s in the output
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all. I don't know from where "None" come. Of course I should like to get rid of them.
class FirstClass:
	def setdata(self, value):
		self.data=value		
	def display(self):
		print(self.data)
		
x=FirstClass()
y=FirstClass()
x.setdata("King Arthur")
y.setdata(3.14159)
print(x.display())     #King Arthur
print(y.display())     #3.14159
x.data="Age of King Arthur"
print(x.display())     #Age of King Arthur
x.anotherInteger=67
print(x.anotherInteger)   # 67
And now the Output
Output:
λ python class1.py King Arthur None 3.14159 None Age of King Arthur None 67
You get None printed because FirstClass.display function does not return anything so it does return the default None.
In your code you print from within the function, so there is no need to print again when calling the function, e.g.
replace print(x.display()) with just x.display()