2015-02-09 5 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

Ваш отступ кода - это немного беспорядок. Вы можете исправить это;). –

+0

Ваш код * очень, очень нечистый *. Если у вас есть проблемы с отладкой, вы должны действительно спросить себя, как вы можете упростить все. Думаю, вы не пользуетесь всеми этими вложенными процедурами. Возможно, это помогает иметь чистый дизайн OO. – runDOSrun

+0

Спасибо за ответы и предложения, я думаю, что я дам ему еще несколько попыток вывести его, а затем снова его восстановить –

ответ

2

ошибка означает, что self.ecoList[self.temP] является None, хотя ваш код не ожидает, что это будет None. Таким образом, это мешающий линия:

self.ecoList[self.temP].nP = tempAni.p 

Ваш код действительно хочет назначить tempAni.p к None.nP. None не имеет такого атрибута, и именно поэтому вы получаете ошибку. Строка, в которой код генерирует исключение, является лишь признаком того, что в вашем коде что-то не так, где-то. Находить эту ошибку сейчас - ваша задача.

Вам нужно спокойно дышать и поэтапно определять, где ваш код неправильный. Это может быть где угодно, и никто здесь, на SO, не найдет это для вас. Добавьте в свой код инструкции печати и/или утверждения, и сузите проблему. Это отладочная работа, и вам нужно пройти ее!

+1

Спасибо Ян-Филлип за руководство, я после опыта, я дам больше время на это тогда, спасибо :) –

+0

Спасибо за отзыв. Да, на самом деле нет ярлыков для того, чтобы стать опытным разработчиком, а изучение собственных ошибок - ключевой компонент. То есть каждый опытный разработчик, несомненно, придумал бесконечные отладочные сессии кода, которые они сами написали, только чтобы найти позорную ошибку. Он просто принадлежит к игре. –

Смежные вопросы