0

As an assignment, I'm trying to create two classes: One class, Book, looks to see if a book is checked out and returns the title, author and page numbers in a book (these are input variables) and the other class, called Library, adds title - author pairs to a dictionary and sees if some particular book is checked out or not. I keep getting an error message every time I try to run it. How do I fix this strange error?

Here is my code:

class Book:
    def __init__(self, title, author, pages):
        self.title = title
        self.author = author
        self.pages = pages
        self.checkedOut = False

    def checked_Out(self):
        print(self.checkedOut)
        return self.checkedOut

    def change_value_of_checkedOut(self):
        if self.checkedOut == False:
            self.checkedOut = True
            print("Switched from False to True.")
        elif self.checkedOut == True:
            self.checkedOut = False
            print("Switched from True to False.")

    def return_pages(self):
        print(self.pages)
        return self.pages

    def return_title(self):
        print(self.title)
        return self.title

class Library:
    def __init__(self):
        collection = {}

    def addExistingBook(self, book):
        collection[book.title] = book.author

    def addNewBook(self, title, author, pages):
        new_book = Book(title, author, pages)
        collection[title] = new_book.author

    def change_checked_out_status(self, title):
        if title in collection.keys():
            title.change_value_of_checkedOut()
        else:
            print("This book is not in the collection.")

def main():
    title = str(input("Enter the title of the book. "))
    author = str(input("Enter the author of the book. "))
    pages = int(input("Enter the number of pages in the book. "))
    myBook = Book(title, author, pages)
    myLib = Library()
    myLib.addExistingBook(myBook)

main()

Here is what happens when I try to run it:

Enter the title of the book. The Count of Monte Cristo
Enter the author of the book. Alexandre Dumas
Enter the number of pages in the book. 1250
Traceback (most recent call last):
  File "C:/Python33/Class Programs/book_library_classes.py", line 56, in <module>
    main()
  File "C:/Python33/Class Programs/book_library_classes.py", line 54, in main
    myLib.addExistingBook(myBook)
  File "C:/Python33/Class Programs/book_library_classes.py", line 36, in addExistingBook
    collection[book.title] = book.author
NameError: global name 'collection' is not defined
2
  • In your __init__ collection is a local variable. You need to do self.collection for it to be an instance variable
    – RedBaron
    Apr 24, 2013 at 4:50
  • You need to change collection to self.collection at all the places. Apr 24, 2013 at 4:51

6 Answers 6

2

You defined collection as a local variable in __init__:

def __init__(self):
    collection = {}

But that doesn't magically make it an instance variable. You have to do that explicitly:

class Library:
    def __init__(self):
        self.collection = {}

    def addExistingBook(self, book):
        self.collection[book.title] = book.author

Also, I wouldn't make methods like this:

def return_title(self):
    print(self.title)
    return self.title

They're just another layer of obfuscation over the straightforward book.title attribute.

Also, you don't need to write .keys(). if key in dictionary is the preferred syntax:

if title in self.collection:
1

Add self.collection where ever you are referencing collection.

1

Class members must be accessed using self.propertyName. Your code should look like this:

class Library:
    def __init__(self):
        self.collection = {}

    def addExistingBook(self, book):
        self.collection[book.title] = book.author

     ....
0

In your Collection class you have to use internal collection:

class Library:
    def __init__(self):
        self.collection = {}

    def addExistingBook(self, book):
        self.collection[book.title] = book.author

Also the last line looks suspicious. Did you mean this:

self.collection[book.title] = book
0

collection is a local variable

Try referencing it in your functions with self.collection, which should fix your problem.

0

you should tell python that collection belongs to the Library instance, myLib:

modify the Library Class to say: self.collection everywhere you currently have collection i.e.

class Library:
    def __init__(self):
        self.collection = {}

    def addExistingBook(self, book):
        self.collection[book.title] = book.author

    def addNewBook(self, title, author, pages):
        new_book = Book(title, author, pages)
        self.collection[title] = new_book.author

    def change_checked_out_status(self, title):
        if title in self.collection.keys():
            title.change_value_of_checkedOut()
        else:
            print("This book is not in the collection.")

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.