Python child class 错误
Python child class errors
我是 python 的新手,正在为一个学校项目编写此代码。我收到了一些错误消息并且在更正它们时遇到了问题。任何指导将不胜感激。
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)
代码只有 运行 第 13 行并给我错误:
追溯(最近一次通话):
文件“C:/Users/Owner/PycharmProjects/pythonProject/main.py”,第 13 行,位于
Silverback = 大猩猩(巴尼,大,咕噜声)
NameError:名称 'Barney' 未定义
因为它正在搜索名为 Barney 的变量,而没有您定义的变量。
对于参数,您必须在初始化对象之前用引号传递或定义
silverback = Gorilla("Barney", "large", "grunts")
要打印它,请不要使用 class 名称,请使用您初始化对象的变量。
Print(" the Gorilla type is "+silverback.type)
我是 python 的新手,正在为一个学校项目编写此代码。我收到了一些错误消息并且在更正它们时遇到了问题。任何指导将不胜感激。
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)
代码只有 运行 第 13 行并给我错误: 追溯(最近一次通话): 文件“C:/Users/Owner/PycharmProjects/pythonProject/main.py”,第 13 行,位于 Silverback = 大猩猩(巴尼,大,咕噜声) NameError:名称 'Barney' 未定义
因为它正在搜索名为 Barney 的变量,而没有您定义的变量。 对于参数,您必须在初始化对象之前用引号传递或定义
silverback = Gorilla("Barney", "large", "grunts")
要打印它,请不要使用 class 名称,请使用您初始化对象的变量。
Print(" the Gorilla type is "+silverback.type)