首先,我想說,即時通訊noob當涉及到代碼。我在學校攻讀CS學位和Python是我的第一語言。我應該創建一個動物園類,根據我提供的參數輸出信息。由於某種原因,我不能得到這個編譯和即時通訊甚至不知道我做任何正確的事情。我的老師老師正在度假,所以子老師真的很有幫助。謝謝你們爲什麼功能沒有定義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")
尼斯點擊標題... –