简体   繁体   English

Python 列表 object 没有属性 position

[英]Python List object has no attribute position

I receive this error: AttributeError: 'list' object has no attribute 'Pozitie' when I try to run my code, I have a list inside of a class and inside of it I have objects of another class, which needs three parameters: two tuples and a list, I'm trying to pass as a parameter the list inside my first class.我收到此错误: AttributeError: 'list' object has no attribute 'Pozitie'当我尝试运行我的代码时,我在 class 中有一个列表,其中我有另一个 ZA2F2ED4F8EBC2CBB64C21A29DZ 的对象,这需要三个参数两个:元组和列表,我试图将我的第一个 class 中的列表作为参数传递。 I'm pretty new to python and I've tried everything I could think of, some help would be appreciated thanks.我对 python 很陌生,我已经尝试了我能想到的一切,感谢您的帮助。 Here is my code, sorry for the variable names not being english:这是我的代码,很抱歉变量名不是英文:

class StareJoc():
  def __init__(self): 
    
    self.Pozitie = [  
        
      ["Negru_Tura", "Negru_Cal", "Negru_Nebun", "Negru_Regina", "Negru_Rege", "Negru_Nebun",   "Negru_Cal", "Negru_Tura"],
       
      ["Negru_Pion", "Negru_Pion", "Negru_Pion", "Negru_Pion", "Negru_Pion", "Negru_Pion", "Negru_Pion","Negru_Pion"],
        
      ["--", "--", "--", "--", "--", "--", "--", "--"],
        
      ["--", "--", "--", "--", "--", "--", "--", "--"],
        
      ["--", "--", "--", "--", "--", "--", "--", "--"],
       
      ["--", "--", "--", "--", "--", "--", "--", "--"],
        
      ["Alb_Pion", "Alb_Pion", "Alb_Pion", "Alb_Pion", "Alb_Pion", "Alb_Pion", "Alb_Pion", "Alb_Pion"],
       
      ["Alb_Tura", "Alb_Cal", "Alb_Nebun", "Alb_Regina", "Alb_Rege", "Alb_Nebun", "Alb_Cal", "Alb_Tura"]]
      
   def Toate_Mutarile_Posibile(self):
    mutari = [Mutare((6, 4), (4, 4), self.Pozitie)]  #<--- I want to pass in the list "Pozitie" from above, 
                                                     #but I get the error 'list' object has no attribute 'Pozitie'
    for rand in range(len(self.Pozitie)):
        for coloana in range(len(self.Pozitie[rand])):
            randul = self.Pozitie[rand][coloana][0]
            if (randul == 'A' and self.Mutare_Alb) or (randul == 'N' and not self.Mutare_Alb):
                piesa = self.Pozitie[rand][coloana]
                if piesa.find('Pion') != -1:
                    self.MutariPion(rand, coloana, mutari)
                elif piesa.find('Tura') != -1:
                    self.MutariTura(rand, coloana, mutari)
    return mutari

class Mutare():

   def __init__(self, Patrat_Start, Patrat_Destinatie, Pozitie_Curenta): #args: self, tuple, tuple, list
    #(some code)

First, try to write the code in english (chances are you will find other conationals to respond to you are pretty low).首先,尝试用英文编写代码(你会发现其他国家回应你的机会很低)。 Second, at this line: if (randul == 'A' and self.Mutare_Alb) or (randul == 'N' and not self.Mutare_Alb) you are calling self.Mutare_alb , you never assign this variable in the constructor.其次,在这一行: if (randul == 'A' and self.Mutare_Alb) or (randul == 'N' and not self.Mutare_Alb)你正在调用self.Mutare_alb ,你永远不会在构造函数中分配这个变量。 If you have defined this function or variable in some code you didn't post then I don't get any error complaining about the field Pozitie , so it messes up somewhere else.如果您在某些未发布的代码中定义了此 function 或变量,那么我不会收到任何抱怨字段Pozitie的错误,因此它会在其他地方搞砸。

In your code define the constant outside the init _ method:在您的代码中定义init _ 方法之外的常量:

class StareJoc():
  Pozitie = [  
    
  ["Negru_Tura", "Negru_Cal", "Negru_Nebun", "Negru_Regina", "Negru_Rege", "Negru_Nebun",   "Negru_Cal", "Negru_Tura"],
   
  ["Negru_Pion", "Negru_Pion", "Negru_Pion", "Negru_Pion", "Negru_Pion", "Negru_Pion", "Negru_Pion","Negru_Pion"],
    
  ["--", "--", "--", "--", "--", "--", "--", "--"],
    
  ["--", "--", "--", "--", "--", "--", "--", "--"],
    
  ["--", "--", "--", "--", "--", "--", "--", "--"],
   
  ["--", "--", "--", "--", "--", "--", "--", "--"],
    
  ["Alb_Pion", "Alb_Pion", "Alb_Pion", "Alb_Pion", "Alb_Pion", "Alb_Pion", "Alb_Pion", "Alb_Pion"],
   
  ["Alb_Tura", "Alb_Cal", "Alb_Nebun", "Alb_Regina", "Alb_Rege", "Alb_Nebun", "Alb_Cal", "Alb_Tura"]]

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

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