简体   繁体   中英

Why do I keep getting the error: AttributeError: 'int' object has no attribute 'subject'

So I am creating a code what are the co requisites and which courses students can take next by specifying a specific course. For example, if the user inputs "ECE 256", then co requisites should be ECE 256L and the next class they can take is ECE 304.

List.print_list()

File /Users/marleneterrones/Dropbox/ECE 480 Group/linked list/node.py , line 45, in,

 print_list result = result + str(dataObj.subject) AttributeError: 'str' object has no attribute 'subject'
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
  1. dataObj = currentNode.subject makes dataObj maybe an int , then dataObj.subject causes such an error. You may just want

    result = result + str(dataObj)
  2. there are some needless code in you snippet, eg:

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

    the 2 latter lines are not necessary since you've already initialized node with subject and corec .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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