School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,148 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,730 people online right now. Registration is fast and FREE... Join Now!




Class issue

 

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

brramiz

8 Sep, 2009 - 02:53 PM
Post #1

New D.I.C Head
*

Joined: 14 Apr, 2009
Posts: 11


My Contributions
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

CODE

[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:

CODE

[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

CODE

[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]





CODE

[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]
        


User is offlineProfile CardPM
+Quote Post


Stutzbach

RE: Class Issue

8 Sep, 2009 - 03:18 PM
Post #2

New D.I.C Head
*

Joined: 6 Jul, 2008
Posts: 29



Thanked: 5 times
My Contributions
QUOTE(brramiz @ 8 Sep, 2009 - 04:53 PM) *

CODE

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



I think it should be:

CODE

patron._booksOut += 1



User is offlineProfile CardPM
+Quote Post

code_m

RE: Class Issue

8 Sep, 2009 - 03:24 PM
Post #3

D.I.C Head
**

Joined: 21 Apr, 2009
Posts: 124



Thanked: 8 times
My Contributions
Remember to use self when need:

python

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
User is offlineProfile CardPM
+Quote Post

brramiz

RE: Class Issue

8 Sep, 2009 - 06:20 PM
Post #4

New D.I.C Head
*

Joined: 14 Apr, 2009
Posts: 11


My Contributions
thanks guys you both were really helpful, those small changes corrected the problems i was having so again thanks!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 04:11PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month