2015-02-09 140 views
-1

我是新来的Python /编程,并试图解决一些问题,以获得它的挂钩。 我一直在努力与下面的错误,并不太确定我是如何得到它。我知道这是说文件类型是None,这就是为什么它会抛出错误。然而,我不明白这是怎么一回事呢?想知道我能否就这个问题得到一些指导或暗示?非常感谢你,对不起,如果代码是凌乱python - 'NoneType'对象没有任何属性

line 138, in move self.ecoList[self.temP].nP = tempAni.p AttributeError: 'NoneType' object has no attribute 'nP'

from random import randint 
class Bears: 
    def __init__(self): 
    self.p = 0 
    self.dp = 0 
    self.nP = 0 
    self.check = True 

    def run(self): 
    self.dp = randint(-1, 1) 
    self.nP = self.dp + self.p 
    self.check = False 
    def __str__(self): 
    return "Bear_" 
    def __repr__(self): 
    return self.__str__() 
class Fish: 
    def __init__(self): 
    self.p = 0 
    self.dp = 0 
    self.nP = 0 
    self.check = True 

    def run(self): 
    self.dp = randint(-1, 1) 
    self.nP = self.dp + self.p 
    self.check = False 
    def __str__(self): 
    return "Fish|" 
    def __repr__(self): 
    return self.__str__() 
class EcoSystem: 
    def __init__(self): 
    self.ecoList = [None] * 10 
    self.bearList = [] 
    self.fishList = [] 

    for i in range(2): 
     self.bearList.append(Bears()) 
     #Adding bear to ecoList 
     while True: 
      index = randint(0, 9) 
      if self.ecoList[index] is None: 
       self.ecoList[index] = self.bearList[i] 
       self.ecoList[index].p = index 
       break 
      else: 
       continue 
    for i in range(2): 
     self.fishList.append(Fish()) 
     #Adding fish to ecoList 
     while True: 
      index = randint(0, 9) 
      if self.ecoList[index] is None: 
       self.ecoList[index] = self.fishList[i] 
       self.ecoList[index].p = index 
       break 
      else: 
       continue 

    self.move() 

    def move(self): 
    #Print out the current eco system 

    print(*self.ecoList, sep='\n') 
    anwser = True 
    while anwser: 


     #populate next move new position for each object 
     for i in range(len(self.ecoList)): 
      if self.ecoList[i] is None: 
       continue 
      else: 
       self.ecoList[i].run() 
     #run for loop to test next position of the object 
     for i in range (len(self.ecoList)): 
      #if [i] item is None skip to next loop 
      if self.ecoList[i] is None: 
       continue 
      elif self.ecoList[i].check == True: 
       continue 
      #else check if it is going to move then check adjacent slot is going to be taken 
      else: 
       tempAni = None #temp animal to compare with item in loop 
       #call out new position from item i 
       newP = self.ecoList[i].nP 
       #call out direction: 
       newDP = self.ecoList[i].dp 
       #do nothing, skip to next slot if it is not going to move 
       if newDP == 0: 
        self.ecoList[i].check = True 
        continue 
       elif newDP != 0:#test if new position is going to be out of bound 
        if newP < 0 or newP > (len(self.ecoList)-1): 
         #set new position back to current 
         self.ecoList[i].nP = self.ecoList[i].p 
         self.ecoList[i].dp = 0 
         self.ecoList[i].check = True 
        else: 
         #test if new position is going to be collided 
         if self.ecoList[newP] is not None: 
          if self.ecoList[newP].nP == self.ecoList[i].nP: 
           print("////////////////") 
           tempAni = self.ecoList[newP] 
          #test if the next next new position is not None or out of bound 
          #Assumption - prioritize the closet animal going to move 
          elif (newP+newDP) > 0 and (newP+newDP) < (len(self.ecoList)-1): 
           if self.ecoList[newP+newDP] is not None: 
            #test if this is going to be collided 
            if self.ecoList[newP+newDP].nP == self.ecoList[i].nP: 
             print("\\\\\\\\\\\\\\") 
             tempAni = self.ecoList[newP+newDP] 
       #if tempAni is not none compare the type 
       if tempAni is not None: 
        print ("####") 
        print (self.ecoList[i].p) 
        print (self.ecoList[i]) 
        print("-----------") 
        print (tempAni.p) 
        print(tempAni) 
        print ("####") 
        #test if they are the same type 
        self.temP = tempAni.p 
        if tempAni.__class__.__name__ == self.ecoList[i].__class__.__name__: 
         #if they are, change new position to current position 
         self.ecoList[i].nP = self.ecoList[i].p 
         self.ecoList[i].check = True 
         print("?????") 
         print(self.temP) 
         print(tempAni) 
         print(tempAni.dp) 
         print(self.ecoList[i]) 
         print(self.ecoList[i].dp) 
         self.ecoList[self.temP].nP = tempAni.p 
         self.ecoList[self.temP].check = True 
         #create new animal of the same type and put it to a random place on the list 
         #Assumption - if the list is full add do nothing 
         #Determine tempAni type to create new bear or fish 
         if isinstance(tempAni, Bears): 
          #create new bear 
          newAni = Bears() 
         else: 
          #creaete new fish 
          newAni = Fish() 
         #while loop if the list is still have available spot add new animal to random spot, otherwise do nothing 
         while None in self.ecoList: 

          index = randint(0, 9) 
          if self.ecoList[index] is None: 
           self.ecoList.insert(index, newAni) 
           self.ecoList[index].p = index 
           print ("*****") 
           print (self.ecoList[index].p) 
           print (self.ecoList[index]) 
           print ("*****") 
           break 
        #if they are not the same type, kill the fish 
        else: 
         #determine if tempAni is the fish or bear 
         if isinstance(tempAni, Bears): 
          #if it is bears kill the fish in i 
          self.ecoList[i].p = -1 
          self.ecoList[i].check = True 
          self.ecoList[self.temP].check = True 
         elif isinstance(tempAni, Fish): 
          #if it is fish kill it 
          self.ecoList[self.temP].p = -1 
          self.ecoList[i].check = True 
          self.ecoList[self.temP].check = True 

     #Apply the change after all the checks are finished 
     #Remove all the fish got killed and apply the moves 
     for i in range (len(self.ecoList)): 
      if self.ecoList[i] is not None: 

        if self.ecoList[i].p == -1: 
         self.ecoList[i] = None 
        elif self.ecoList[i].check == False: 
         self.ecoList[i].check = True 
         newP = self.ecoList[i].nP 
         if newP != i: 
          self.ecoList[newP] = self.ecoList[i] 
          self.ecoList[newP].p = newP 
          self.ecoList[i] = None 

     #Print out the current eco system 
     print ("---------------------------------------") 
     for i in range (len(self.ecoList)): 
      print(self.ecoList[i]) 
      print(i) 
     #Ask if user want to continue playing 
     test = True 
     while test == True: 
      strAns = input ('Enter y/n to continue or not: ') 
      if strAns.lower() == "n": 
       anwser = False 
       test = False 
       break 
      elif strAns.lower() == "y": 
       test = False 
       break 
def main(): 
EcoSystem() 

main() 
+3

你的代码缩进有点乱。你可能想修复它;)。 – 2015-02-09 14:23:37

+0

你的代码*非常非常不洁净*。如果你在调试时遇到困难,你应该问自己如何简化一切。我认为你不是在帮助所有那些嵌套的程序性陈述。也许它有助于有一个干净的面向对象设计。 – runDOSrun 2015-02-09 14:31:49

+0

谢谢你的回复和建议,我想我会再给它几次尝试,然后再次重建它 – 2015-02-09 15:08:57

回答

2

的错误意味着self.ecoList[self.temP]None,但您的代码不希望它是None。所以,这是有问题的行:

​​

你的代码实际上要分配给tempAni.pNone.nPNone没有这样的属性,这就是为什么你会得到错误。代码引发异常的行只​​是表示代码中有某些错误,某处。现在找到这个bug是你的任务。

你需要平静地呼吸,并一步一步地找出你的代码错误的地方。这可能在任何地方,没有人会在这里为你找到这个。向您的代码添加打印和/或声明语句,并缩小问题范围。这是调试工作,你必须经历它!

+1

感谢Jan-Phillip的指导,我经历了毕竟的经验,我会给更多的时间就此,谢谢:) – 2015-02-09 15:11:22

+0

感谢您的反馈意见。是的,实际上在成为有经验的开发人员方面没有捷径,并且从自己的错误中学习是一个至关重要的组成部分。也就是说,每个有经验的开发人员肯定会自己写下自己编写的代码进行无尽的调试会议,只是为了找到一个可耻的错误。它只属于游戏。 – 2015-02-09 16:04:15

相关问题