2015-05-07 170 views
-3

首先,我想說,即時通訊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") 
+2

尼斯點擊標題... –

回答

0

你玩弄你的__init__()定義,然後你打電話時,他們失去了一些東西。

如果要以標準方式創建各種動物對象,則必須以標準方式定義它們。使用kid, animalType, legs, age, featurekid, legs, age, animalType, feature不會允許您這樣做(並且我建議使用標準self來引用對象的實例,而不是kid)。

當您嘗試創建動物時,您正在提供它的類型,腿的數量和年齡,但是您從未告訴構造函數feature是什麼。其他人在程序的頂部附近定義(順便提一下,我建議將類定義從循環中取出),但feature無處可查。以某種方式定義它,或者完全忽略對feature(即使用類似kid1= Cub(kind, legs, age)之類的內容)的引用以降低默認值。