Classes, Subclass and None Error message. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Classes, Subclass and None Error message.

Hi, so I wrote the following code... class Animal: def noise(self): print("noise") class Dog(Animal): def noise(self): print("woof") class Hog(Animal): def noise(self): print("snort") print(Animal().noise(), "xx ", Dog().noise(),"xx ", Hog().noise()) And the output I get is: noise woof snort None xx None xx None Then when I delete the line of code with the print method, ie class Animal: def noise(self): print("noise") class Dog(Animal): def noise(self): print("woof") class Hog(Animal): def noise(self): print("snort") #print(Animal().noise(), "xx ", Dog().noise(),"xx ", Hog().noise()) Then the output is.. .. there's not output. I do not understand what's going on here In the first code version, I expected the output would be "noise xx bark xx snort". A) Why was the output noise bark snort on separate lines and without the xx? B) Why did None xx None xx None print? C) Why when in the second version and I got rid of the print function, was there no output? I expected it to be noise bark snort on separate lines Thanks!

1st May 2019, 11:55 AM
Josie The Ritalin Dog
Josie The Ritalin Dog - avatar
1 Answer
+ 2
Hi Ariella, A) Because of the call to print in the methods. B) None is automatically returned by a function that doesn't have any other return value. Your methods only print something but don't return anything. C) Because you didn't call the methods. Hope that helps.
1st May 2019, 3:58 PM
Thoq!
Thoq! - avatar