Python Forum
How Can I Resolve This Error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How Can I Resolve This Error
#1
Hi, I am currently trying to code for this particular question which is
Uncle Tim started his Secondhand Bookstore in 2000. As years passed, the number of books in his store increased significantly. He can no longer keep track of their location in his store. He has approached you to develop a program (in Python) that could help him to keep track of the books.
My Current Codes Are :
class Node:
    def __init__(self,data):
        self.data = data
        self.next = None

    def getData(self):
        return self.data

    def setData(self, newData):
        self.data = newData

    def getNext(self):
        return self.next

    def setNext(self,newNode):
        self.next=newNode

class Book:
    def __init__(self, author, title, price):
        self.author = author
        self.title = title
        self.price = price
        
    def __str__(self):
        return 'Author: ' + self._author + ', Title: ' + \
            self._title + ', Price: ' + str(self._price);
        
    def setAuthor(self, author):
        self._author = author
        
    def getAuthor(self):
        return self.author
        
    def setTitle(self, title):
        self._book = book
        
    def getTitle(self):
        return self.title

    def setPrice(self, price):
        self._price = price
        
    def getPrice(self):
        return self._price

class LinkedList:
    def __init__(self):
        self.head = None
        self.size = 0

    def AddBookToFront(self, data):
        print("1")
        newNode = Node(data)
        newNode.setNext(self.head)
        self.head = newNode
        self.size+=1

    def DisplayBook(self):
        print("3")
        tempo = self.head
        while tempo is not None:
            print ("Author Name: " + tempo.getData().getAuthor() +" " +"Title: " + tempo.getData().getTitle())
            tempo = tempo.getNext()
    
    def RemoveBookAtPosition(self,position):
          # If linked list is empty
          if self.head == None:
              return

          # Store head node
          temp = self.head

          # If head needs to be removed
          if position == 0:
              head = temp.next
              temp = None
              return

          # Find previous node of the node to be deleted
          for i in range(position -1 ):
              temp = temp.next
              if temp is None:
                  break

          # If position is more than number of nodes
          if temp is None:
              return
          if temp.next is None:
              return

          # Node temp.next is the node to be deleted
          # store pointer to the next of node to be deleted
          next = temp.next.next

          # Unlink the node from linked list
          temp.next = None

          temp.next = next
    def AddBookAtPosition(self, data , position):
        cursor= self.head
        temps = Node(data)
        index = 0
        prev = None
        if (position ==0):
            temps.setNext(cursor)
            self.head = temp
        else:
            while (index < position):
                prev = cursor
                cursor = cursor.getNext()
                index +=1
        temps.setNext(prev.getNext)
        prev.setNext(temps)

ll=LinkedList()
book1 = Book("AAA","BBB","$4")
#n1=Node(book1)
book2 = Book("CCC", "BBB", "$5")
#n2=Node(book2)
book3 = Book("DDD", "CCC", "$6")
#n3=Node(book3)
book4 = Book("GGG", "FFF", "$3")

ll.AddBookToFront(book1)
ll.AddBookToFront(book2)
ll.AddBookToFront(book3)
#ll.AddBookAtPosition(book4, 2)
ll.DisplayBook()
print("After Removing")
ll.RemoveBookAtPosition(1)
ll.DisplayBook()
print("Add Book At Position")
ll.AddBookAtPosition(book4, 2)
ll.DisplayBook()

As Far As I Know This Is The Problem Causing Code
[python]
def DisplayBook(self):
        print("3")
        tempo = self.head
        while tempo is not None:
            print ("Author Name: " + tempo.getData().getAuthor() +" " +"Title: " + tempo.getData().getTitle())
            tempo = tempo.getNext()
This Is My Current Output
Output:
1 1 1 3 Author Name: DDD Title: CCC Author Name: CCC Title: BBB Author Name: AAA Title: BBB After Removing 3 Author Name: DDD Title: CCC Author Name: AAA Title: BBB Add Book At Position 3 Author Name: DDD Title: CCC Author Name: AAA Title: BBB Author Name: GGG Title: FFF Traceback (most recent call last): File "c:\Users\TP_baseline\Desktop\DSAG\ProjectTest9-2.py", line 134, in <module> ll.DisplayBook() File "c:\Users\TP_baseline\Desktop\DSAG\ProjectTest9-2.py", line 62, in DisplayBook print ("Author Name: " + tempo.getData().getAuthor() +" " +"Title: " + tempo.getData().getTitle()) AttributeError: 'function' object has no attribute 'getData'
Error Code
Error:
Traceback (most recent call last): File "c:\Users\TP_baseline\Desktop\DSAG\ProjectTest9-2.py", line 134, in <module> ll.DisplayBook() File "c:\Users\TP_baseline\Desktop\DSAG\ProjectTest9-2.py", line 62, in DisplayBook print ("Author Name: " + tempo.getData().getAuthor() +" " +"Title: " + tempo.getData().getTitle()) AttributeError: 'function' object has no attribute 'getData'
Any Guidance On Solving This Issue Would Be Greatly Appreciated.
Thanks In Advance
Reply
#2
Python tells you: function object has no attribute getData at line 62. At this line, you see that the object is tempo. How was tempo defined? At line 61 you have tempo = self.head. It means that self.head is a function object at that point instead of a Node. You probably forgot to call a function in your code.

In order to get more information, add this line in the while section between line 61 and 62:
if not isinstance(tempo, Node): raise RuntimeError((tempo,))
Reply
#3
As a side note
1. you don't need all these getters and setters in Node, Book classes. That is pattern from other languages, not python
2. you may want to explore deque, and implement LinkedList as deque for optimised performance
3.I don't see the purpose of Node class and nextNode methods
Reply
#4
Hi Thanks For The Answers, When testing the system, it seems that when I dont call the AddBookToPosition function, the DisplayBook class works normally.
Thanks
Reply
#5
(Jan-18-2018, 12:50 PM)JayJayOi Wrote: when I dont call the AddBookToPosition function, the DisplayBook class works normally.
At line 112, it seems to me that you need to call prev.getNext()
Reply
#6
(Jan-18-2018, 12:58 PM)Gribouillis Wrote:
(Jan-18-2018, 12:50 PM)JayJayOi Wrote: when I dont call the AddBookToPosition function, the DisplayBook class works normally.
At line 112, it seems to me that you need to call prev.getNext()
Thanks, It worked, would you be able to help me with one last issue ?

With The Above Mentioned issue Solved, I have had another error that has just sprung up .
The Updated Codes Are As Below
class Node:
    def __init__(self,data):
        self.data = data
        self.next = None

    def getData(self):
        return self.data

    def setData(self, newData):
        self.data = newData

    def getNext(self):
        return self.next

    def setNext(self,newNode):
        self.next=newNode

class Book:
    def __init__(self, author, title, price):
        self.author = author
        self.title = title
        self.price = price
        
    def __str__(self):
        return 'Author: ' + self._author + ', Title: ' + \
            self._title + ', Price: ' + str(self._price);
        
    def setAuthor(self, author):
        self._author = author
        
    def getAuthor(self):
        return self.author
        
    def setTitle(self, title):
        self._book = book
        
    def getTitle(self):
        return self.title

    def setPrice(self, price):
        self._price = price
        
    def getPrice(self):
        return self._price

class LinkedList:
    def __init__(self):
        self.head = None
        self.size = 0

    def AddBookToFront(self, data):
        print("1")
        newNode = Node(data)
        newNode.setNext(self.head)
        self.head = newNode
        self.size+=1

    def DisplayBook(self):
        print("3")
        tempo = self.head
        while tempo is not None:
            print ("Author Name: " + tempo.getData().getAuthor() +" " +"Title: " + tempo.getData().getTitle())
            tempo = tempo.getNext()
    
    def RemoveBookAtPosition(self,position):
          # If linked list is empty
          if self.head == None:
              return

          # Store head node
          temp = self.head

          # If head needs to be removed
          if position == 0:
              head = temp.next
              temp = None
              return

          # Find previous node of the node to be deleted
          for i in range(position -1 ):
              temp = temp.next
              if temp is None:
                  break

          # If position is more than number of nodes
          if temp is None:
              return
          if temp.next is None:
              return

          # Node temp.next is the node to be deleted
          # store pointer to the next of node to be deleted
          next = temp.next.next

          # Unlink the node from linked list
          temp.next = None

          temp.next = next
    def AddBookAtPosition(self, data , position):
        cursor= self.head
        temps = Node(data)
        index = 0
        prev = None
        if (position ==0):
            temps.setNext(cursor)
            self.head = temp
        else:
            while (index < position):
                prev = cursor
                cursor = cursor.getNext()
                index +=1
        prev.getNext()
        prev.setNext(temps)
    
    def bubbleSort(self):
        cursor= self.head
        one = cursor.getNext()
        two = one.getNext()
        for i in range(0,self.size()):
            for j in range(i):
                if(one.getData().getAuthor > two.getData().getAuthor()):
                    temp2 = one
                    one = two
                    two =temp2

ll=LinkedList()
book1 = Book("AAA","BBB","$4")
#n1=Node(book1)
book2 = Book("CCC", "BBB", "$5")
#n2=Node(book2)
book3 = Book("DDD", "CCC", "$6")
#n3=Node(book3)
book4 = Book("GGG", "FFF", "$3")

ll.AddBookToFront(book1)
ll.AddBookToFront(book2)
ll.AddBookToFront(book3)
#ll.AddBookAtPosition(book4, 2)
ll.DisplayBook()
print("After Removing")
ll.RemoveBookAtPosition(1)
ll.DisplayBook()
print("After Adding Book At Position ")
ll.AddBookAtPosition(book4,2)
ll.DisplayBook()
print("After Sorting")
ll.bubbleSort()
ll.DisplayBook()
My Output Is
Output:
1 1 1 3 Author Name: DDD Title: CCC Author Name: CCC Title: BBB Author Name: AAA Title: BBB After Removing 3 Author Name: DDD Title: CCC Author Name: AAA Title: BBB After Adding Book At Position 3 Author Name: DDD Title: CCC Author Name: AAA Title: BBB Author Name: GGG Title: FFF [Done] exited with code=0 in 0.13 seconds [Running] python "c:\Users\TP_baseline\Desktop\DSAG\ProjectTest9-2.py" 1 1 1 3 Author Name: DDD Title: CCC Author Name: CCC Title: BBB Author Name: AAA Title: BBB After Removing 3 Author Name: DDD Title: CCC Author Name: AAA Title: BBB After Adding Book At Position 3 Author Name: DDD Title: CCC Author Name: AAA Title: BBB Author Name: GGG Title: FFF After Sorting
My Error Is
Error:
Traceback (most recent call last): File "c:\Users\TP_baseline\Desktop\DSAG\ProjectTest9-2.py", line 147, in <module> ll.bubbleSort() File "c:\Users\TP_baseline\Desktop\DSAG\ProjectTest9-2.py", line 119, in bubbleSort for i in range(0,self.size()): TypeError: 'int' object is not callable
Any Further Guidance Would Be Greatly Appreciated. And I really appreciate the guidance that was given above .
Thanks
Reply
#7
I think python's message is clear enough: you are trying to call an instance of 'int' as if it was a function. Looking at the line displayed, you see that it is self.size. Remove the parentheses there.
Reply
#8
(Jan-18-2018, 01:26 PM)Gribouillis Wrote: I think python's message is clear enough: you are trying to call an instance of 'int' as if it was a function. Looking at the line displayed, you see that it is self.size. Remove the parentheses there.

I have already followed through and removed the parenthesis as you mentioned. But I still dont quite understand the rationale behind the removal of the parenthesis.
This Is My Updated Code
    def bubbleSort(self):
        cursor= self.head
        one = cursor.getNext()
        two = one.getNext()
        for i in range(0,self.size):
            for j in range(i):
                if(one.getData().getAuthor > two.getData().getAuthor()):
                    temp2 = one
                    one = two
                    two =temp2
And The error Output Is
Error:
Traceback (most recent call last): File "c:\Users\TP_baseline\Desktop\DSAG\ProjectTest9-2.py", line 152, in <module> ll.bubbleSort() File "c:\Users\TP_baseline\Desktop\DSAG\ProjectTest9-2.py", line 122, in bubbleSort if(one.getData().getAuthor > two.getData().getAuthor()): TypeError: '>' not supported between instances of 'method' and 'str'
What does this error message indicate ?
Thanks So Much
Reply
#9
line 7, you compare
one.getData().getAuthor which is method
with
two.getData().getAuthor() which is the result of calling that method

i.e. you miss parenthesise on the LHS of the comparison
Reply
#10
The message indicates that python cannot compare a 'method' to a string with the comparison operator >. A method is a function. You get this because you forgot to call the method one.getData().getAuthor(). Note the parentheses.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I use tabs and spaces? (how to resolve error TabError: inconsistent) Onanism 15 5,698 Mar-24-2022, 07:57 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020