2017-06-20 51 views
0

我试图创建一个化学GUI,显示有关每个元素的各种信息。我正在使用类实例列表来打印出这些信息,但我仍然得到一个'list' object has no attribute 'atomic_number'。这是我建立的班级,以及给我错误的代码。Python:无法搜索类实例变量的列表

class ElementInformation(object): 
    def __init__(self, atomic_number, element_name, element_symbol, atomic_weight, melting_point, boiling_point) 
     self.atomic_number = atomic_number 
     self.element_name = element_name 
     self.element_symbol = element_symbol 
     self.atomic_weight = atomic_weight 
     self.melting_point = melting_point 
     self.boiling_point = boiling_point 

def find_element(): 
    update_status_label(element_information, text_entry) 
    # text entry is a text entry field in TKinter 
    # other code in here as well (not part of my question 


def update_status_label(element_instances, text_input): 

    for text_box in element_instances.atomic_number: 
     if text_input not in text_box: 
      # do stuff 
     else: 
      pass 

element_result_list = [*results parsed from webpage here*] 
row_index = 0 
while row_index < len(element_result_list): 
    element_instances.append(ElementInformation(atomic_number, element_name, element_symbol, atomic_weight, melting_point, boiling_point)) 
    # the above information is changed to provide me the correct information, it is just dummy code here 
    row_index += 1 

我的问题是在功能update_status label,特别是for循环。 Python正在抛出一个错误(就像我之前说的),它说'list' object has no attribute 'atomic_number'。对于我的生活,我似乎无法弄清楚什么是错的。谢谢你的帮助!

如果这有什么差别,我使用的Python 3.x的Windows上

回答

1

试试这个:

for element in element_instances: 
    text_box = element.atomic_number: 
    if text_input not in text_box: 
     # do stuff 
    else: 
     pass 

名单 “element_instances” 是一个Python列表。它没有属性“.atomic number”,即使它里面的所有元素都有这样的属性。 Python的for语句将列表的每个元素分配给一个变量 - 该元素是您的自定义类的一个实例,您可以在其中调整属性。

+0

我的互联网连接目前非常缓慢,所以我无法测试任何东西,直到我回到家中,但现在我正在查看它。谢谢! – Goalieman