Class issue

i'm having trouble with a small part of this class, it's one o

Page 1 of 1

3 Replies - 1469 Views - Last Post: 08 September 2009 - 07:20 PM Rate Topic: -----

#1 brramiz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-April 09

Class issue

Post icon  Posted 08 September 2009 - 03:53 PM

i'm writing 2 classes patron.py and book.py i have a good chunk of what i need working i'm having a small problem and i'm sure it's probably syntactical or something just in the wrong place but either way i could really use some help.

what the program does is takes the class patron.py and book.py to keep track of who has checked out what book and how many books they have out and waiting lists for books if someone has it checked out but someone else wants it. that's the basic idea of what it should be doing.

now my problem comes in on the getting the program to borrow a book out to someone, so then it also has the problem of not keeping track of the # of books they have either this is the section of code i'm having trouble with

[color=#000099]def borrowMe(self, patron):
		
		if self._borrower !=None:
			print "you have been put on the waiting list for this book"
			self._waitList.add(patron)
			return False
		elif self._borrower ==None:
			if patron.getNumOfBooksOut()>=3:
				print "you have too many books checked out already"
				return False
			else:
				patron.self._booksOut+=1
				print "You have successfully checked the book out"[/color]



this is how i begin to run it and the error that shows up:

[color=#33CC00]>>> patron1=Patron("Brandon", "Ramirez")
>>> book1=Book("Fundamentals of Python", "Kenneth A. Lambert")
>>> book1.borrowMe(patron1)[/color]

[color=#FF0000]Traceback (most recent call last):
  File "<pyshell#120>", line 1, in <module>
	book1.borrowMe(patron1)
  File "C:\Python26\book.py", line 30, in borrowMe
	patron.self._booksOut+=1
AttributeError: 'Patron' object has no attribute 'self'[/color]



now I know that self._booksOut is a problem but i can't really tell why or how to fix it. if any one can help i would really appreciate it!

i've posted the rest of my code below

[color=#000099]class Patron(object):
	def __init__(self, fName, lName, ):
		self._firstName = fName
		self._lastName = lName
		self._booksOut = 0
		
	def getName(self):
		return self._firstName+ " " +self._lastName

	def getNumOfBooksOut(self):
		return self._booksOut

	def checkOut(self):
		if self._booksOut<3:
			self._booksOut +=1
			return True
		else:
			return False

	def returnBook(self, num):
		self._numReturned = num
		if 0<self._numReturned<=self._booksOut:
			return True
			booksOut-=self._numReturned
		else:
			return False

	def __str__(self):
		return patron1.getName() + " has " + str(self._booksOut) +\
			   " books checked out "[/color]






[color=#000099]from patron import Patron
class Book(object):

	def __init__(self, title, author):
		self._bookTitle = title
		self._bookAuthor = author
		self._waitList=()
		self._borrower=None

	def getTitle(self):
		return self._bookTitle

	def getAuthor(self):
		return self._bookAuthor

	def getPatron(self):
		return self._borrower

	def borrowMe(self, patron):
		
		if self._borrower !=None:
			print "you have been put on the waiting list for this book"
			self._waitList.add(patron)
			return False
		elif self._borrower ==None:
			if patron.getNumOfBooksOut()>=3:
				print "you have too many books checked out already"
				return False
			else:
				patron.self._booksOut+=1
				print "You have successfully checked the book out"[/color]
		



Is This A Good Question/Topic? 0
  • +

Replies To: Class issue

#2 Stutzbach  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 29
  • Joined: 06-July 08

Re: Class issue

Posted 08 September 2009 - 04:18 PM

View Postbrramiz, on 8 Sep, 2009 - 04:53 PM, said:

	patron.self._booksOut+=1
AttributeError: 'Patron' object has no attribute 'self'[/color]



I think it should be:

patron._booksOut += 1


Was This Post Helpful? 1
  • +
  • -

#3 code_m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 202
  • Joined: 21-April 09

Re: Class issue

Posted 08 September 2009 - 04:24 PM

Remember to use self when need:

        if self._borrower != None:
            print "you have been put on the waiting list for this book"
            self._waitList.add(patron)
            return False
        else:
            if self.getNumOfBooksOut() >= 3:
                print "you have too many books checked out already"
                return False
            else:
                self._booksOut += 1
                print "You have successfully checked the book out"[/color]



Note that there is never a need to use elif without an else statement
Was This Post Helpful? 1
  • +
  • -

#4 brramiz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-April 09

Re: Class issue

Posted 08 September 2009 - 07:20 PM

thanks guys you both were really helpful, those small changes corrected the problems i was having so again thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1