Search code examples
python-3.xparent-childsuperclass

Python child class errors


Im very new to python and working on this code for a school project. I'm getting some error messages and having issues correcting them. Any guidance would be much appreciated.

enter code here
class Mammal():
def __init__(self, name='', size='', sound=''):
  self.name = name
  self.size = size
  self.sound = sound

class Gorilla(Mammal):
    def __init__(self, name, size,sound, type):
    super().__init__(name, size, sound)
    self.type = type


Silverback = Gorilla(Barney, large, grunts)
print("The gorilla type is"+ Gorilla.type)
print(" The gorilla's name is"+ Gorilla.name)
print(' The gorilla is'+ Gorilla.size)
print('the gorilla sound is'+ Gorilla.sound )


class Bear(Mammal):
    def __init__(self, name, size,sound, type):
    super().__init__(name, size, sound)
    self.type = type

Polar = Bear(Henry, large, roar)
print("The bear type is"+ Bear.type)
print(" The bear's name is"+ Bear.name)
print(' The bear is'+ Bear.size)
print('the bear says'+ Bear.sound )



b = Bear()
g = Gorilla()
if (b == g):
    print(b.getGenus() + " and " + g.getGenus() + " are of the same genus")
else:
    print(b.getGenus() + " and " + g.getGenus() + " are *not* of the same 
genus")


def listenToMammal(Mammal):
    print(Mammal.makeNoise())


listenToMammal(B)
listenToMammal(G)

The code is only running to line 13 and giving me the error: Traceback (most recent call last): File "C:/Users/Owner/PycharmProjects/pythonProject/main.py", line 13, in Silverback = Gorilla(Barney, large, grunts) NameError: name 'Barney' is not defined


Solution

  • Because of it searching variable called Barney and there' s no variable you defined. For arguments you jhave to pass with quotes or define before you intiliaze the object

    silverback = Gorilla("Barney", "large", "grunts") To print it don't use the class name use the variable you initialized the object.

    Print(" the Gorilla type is "+silverback.type)