繁体   English   中英

python-'NoneType'对象没有属性

[英]python - 'NoneType' object has no attribute

我是Python /编程的新手,正在尝试解决一些小问题以掌握它。 我一直在努力与下面的错误,也不太确定我是怎么得到的。 我知道这是在说文件类型为None,这就是为什么它会引发错误。 但是,我一开始不了解None。 只是想知道我是否可以在这个问题上得到一些指导或提示? 非常感谢您,如果代码混乱

第138行,在移动self.ecoList [self.temP] .nP = tempAni.p AttributeError:'NoneType'对象没有属性'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()

该错误意味着self.ecoList[self.temP]None ,尽管您的代码不希望它为None 因此,这是令人反感的行:

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

您的代码实际上想要将tempAni.p分配给None.nP None一个没有这样的属性,这就是为什么出现错误的原因。 代码引发异常的行仅表示代码中某处有问题 查找此错误是您现在的任务。

您需要冷静地呼吸,并逐步找出代码错误的地方。 它可能在任何地方,所以这里没有人会为您找到这个。 在代码中添加print和/或assert语句,并缩小问题范围。 这是调试工作,您必须完成它!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM