2014-03-07 135 views
0

因此,我正在創建一個代碼,通過指定一個特定的課程,學生可以接受哪些必修課程和哪些課程。例如,如果用戶輸入「ECE 256」,然後共同要件應ECE 256L,他們可以採取下一個類是ECE 304爲什麼我不斷收到錯誤:AttributeError:'int'object has no attribute'subject'

List.print_list() 

文件「/用戶/ marleneterrones /升降梭箱/ ECE 480組/鏈表/ node.py」,第45行,在print_list 結果=結果+ STR(dataObj.subject) AttributeError的: 'STR' 對象沒有屬性 '主體'

class Node: 
    def __init__(self, subject=None, corec=[] , next_class=[]): 
     self.subject = subject   
     self.corec = corec    
     self.next_class = next_class 

class LinkedList: 
    def __init__(self): 
     self.firstNode = Node(None, None, None)  
     self.lastNode = self.firstNode    
     self.size = 0       

    def add(self, subject,corec): 
     """Add a node to the list""" 
     node = Node(subject , corec ,None) 
     node.subject = subject; 
     node.corec = corec; 

     if self.firstNode.subject == None: 
      self.firstNode = node 
      self.lastNode = node 
     else: 
      self.lastNode.next_class = node 
      self.lastNode = node 

     self.size += 1 

    def print_list(self): 
     """prints whats ever in the array """ 
     result = "" 
     currentNode = Node(None,None, None) 
     currentNode = self.firstNode 
     i = 0 

     result = result + "(" 

     while currentNode != None: 
      if i > 0: 
       result = result + "," 

      dataObj = currentNode.subject 
      dataObj2 = currentNode.corec 

      if dataObj != None: 
       result = result + str(dataObj.subject) 

      if dataObj2 != None: 
       result = result + str(dataObj2.corec) 

      currentNode = currentNode.next_class 

      i = i + 1 
     result = result + ")" 
     return result 

回答

0
  1. dataObj = currentNode.subject使得dataObj可能是int ,那麼dataObj.subject會導致這樣的錯誤。你可能只是想

    result = result + str(dataObj) 
    
  2. 有一些不必要的代碼片斷你,例如: -

    node = Node(subject , corec ,None) 
    node.subject = subject; 
    node.corec = corec; 
    

    2條後者線是沒有必要的,因爲你已經有subjectcorec初始化node

+0

非常感謝! – user2669764

相關問題