Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I fix these errors for a short assignment in creating classes in Python?
    primarykey
    data
    text
    <p>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:</p> <p>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". </p> <p>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. </p> <p>Finally the library class will be implemented in a python program called main.py.</p> <p>Here is my code:</p> <pre><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.") </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload