首先,在谈到代码时,我想说的是一个总的菜鸟。我在学校攻读CS学位,python是我的第一语言。我应该创建一个动物园类,根据我提供的参数打印出信息。由于某种原因,我无法得到这个编译,我甚至不确定我正在做什么。我的老师老师正在度假,所以副手真的很有帮助。谢谢你们
import random
question = 'y'
while question == 'y':
#animals we want are lion, turtle, kangaroo,
kind=['lion','turtle', 'kangaroo', 'flamingo', 'pinguin', 'bear']
animalType= random.choice (kind)
legs= str(random.randint(0,4))
age= str(random.randint(1,70))
class Parent:
""" superclass for a Zoo of animals """
def __init__(self, kind, legs, age):
self.animalType = animalType
self.legs = legs
self.age = age
def getanimal (self):
return self.animalType
def getlegs (self):
return self.legs
def getage (self):
return self.age
def __str__(self):
output = 'Kind of Animal {}, Number of legs {}, Age {} '.format(self.animalType, self.legs, self.age)
return output
def makeNoise(self):
return None
class Cub(Parent):
"""subclass of parent"""
feature='playful'
def __init__(kid, animalType, legs, age, feature="playful"):
super(Parent).__init__(animalType, legs, age)
kid.feature = feature
def getplayful (kid):
return kid.feature
def __str__(kid,):
parentOutput = super(Parent).__str__()
output = 'cub traits {} {}'.format(
parentOutput, kid.feature)
return output
def makeNoise(kid):
if make_Noise is True:
print('ROAR')
print()
class babyTurtle (Parent):
"""subclass of parent"""
slow= "slow"
def __init__(kid, animalType, legs, age, feature = 'slow'):
super(Parent).__init__(animalType, legs, age)
kid.feature = feature
def getslow (kid):
return kid.feature
def __str__(kid):
parentOutput = super(Parent).__str__()
output = 'babyTurtle traits {} {}'.format(
parentOutput, kid.feature)
return output
def makeNoise(kid):
if make_Noise is True:
print('do baby turtles even make noise?')
print('')
class marsupial(Parent):
"""subclass of Parent"""
def __init__(kid, legs, age, animalType, feature='kanga jack is gonna kick you out'):
super(Parent).__init__(animalType, legs, age)
kid.feature = feature
def getkanga (kid):
return kid.feature
def __str__(kid):
parentOutput = super(Parent).__str__()
output = 'kanga traits {} {} '.format(
parentOutput, self.kanga)
return output
def makeNoise(kid):
if make_Noise is True:
print('the name is Jack')
print('')
animal1 = Parent(kind, legs, age)
Zoo=[]
Zoo.append(animal1)
answer = input("add another animal y/n: ")
if answer == question:
question = 'y'
elif answer == 'n':
kid1= Cub(kind, legs, age, feature)
kid2= babyturtle(kind, legs, age, feature)
kid3= marsupial(kind, legs, age, feature)
print (Zoo)
print (kid1, True.makeNoise)
print (kid2, True.makeNoise)
print (kid3, True.makeNoise)
question = 'n'
else:
print("error")
答案 0 :(得分:0)
您使用__init__()
定义快速放松,然后在调用时遗漏了一些内容。
如果您想以标准方式创建各种动物对象,则必须以标准方式定义它们。使用kid, animalType, legs, age, feature
但kid, legs, age, animalType, feature
将不允许您这样做(我建议使用标准self
来引用对象的实例,而不是kid
)。
当你尝试制作动物时,你会提供它的类型,腿数和年龄,但你从未告诉过构造函数feature
是什么。其他的是在程序顶部附近定义的(我建议顺便从循环中取出类定义),但feature
无处可寻。以某种方式定义它,或者完全忽略对feature
的引用(即,使用kid1= Cub(kind, legs, age)
之类的内容)以降至默认值。