2017-06-20 47 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