Ivan Lesiv
Ivan Lesiv

Reputation: 59

How do I fix these errors for a short assignment in creating classes in Python?

I have a short assignment I need to complete that has to do with creating classes, and I'm not sure what the problem with my code is. Here are the instructions below and underneath that is my code. Please explain what my error(s) is/are and how to fix it/them:

Build 2 classes. The first class will be a "Book" class. The book class has 4 variables that are all private. The first is checkedOut which is a Boolean value initialized to false, title which is a string that is initalized by a input variable, author which is a string and is initialized by an input variable, pages that is an integer and is also initialized by an input variable. This class will also have 4 functions associated with is. The first will return the variable checkedOut. The second will change the value of checkedOut. If the value is set to true, then it will be changed to false and vice versa. The third function will return the number of pages and the final function will return the title. When a book object is printed it be in the format of "title author pages checkedOut".

The second class will be called a library. When the library is initialized it will create a empty dictionary called collection. The library class will have 2 functions. The first function will be called addBook and it will take in 3 input variables, title, author, and pages. In this function you will create a book object, then add it to the dictionary with the title as the key. The second function will take in the title of a book, find the book in the dictionary and call the books function that changes the checkedOut status. The finally, when a library object is printed out, it will print out each book in the library on a separate line.

Finally the library class will be implemented in a python program called main.py.

Here is my code:

class Book:

    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. "))
    checkedOut = False

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

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

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

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



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

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

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

What is wrong with what I did here? I keep getting errors to the effect that a certain variable name is not defined when I try to create objects and run the code in IDLE.

Upvotes: 0

Views: 884

Answers (2)

Rushy Panchal
Rushy Panchal

Reputation: 17532

Create the class separately, and get the input separately. Also, you want to mention that each variable is an instance variable, not in the local scope:

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) # it has to be the instance variable
        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): # create a book
        new_book = Book(title, author, pages)
        collection[title] = new_book.author # access the 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.")

Then, add the rest in a main function:

def main():
    # if you are using Python 2.x, change input() to raw_input()
    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)

Upvotes: 0

CppLearner
CppLearner

Reputation: 17040

(0) Next time, paste the errors.

class Book:

    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. "))
    checkedOut = False

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

(1) Just this part.

checkedOut is not defined. Where do you see checkedOut? right above the function checked_Out. Okay. Short answer, add self..

Example:

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

And you really should not be doing the title, author stuff there. They become class variables, not instance variables. There's a difference.

(2) Avoid input if you are still using 2.x Python.

Use raw_input and get rid of str. That's safer. In the 3.x you can use input and it will always be string (raw_input will always return string). That's also causing problem.

(3) You have the tenadacy of captializing everything. I am usually pretty chill but it kinds of bad. Don't call it checked_Out that's really inconsistent plus Python programmers prefer checked_out. checkedOut can be named to checked_out and hey, you can conflict. Don't name your function and variables so similar.

Upvotes: 3

Related Questions