0

我有一个简短的任务需要完成,这与创建类有关,我不确定我的代码有什么问题。这是下面的说明,下面是我的代码。请解释我的错误是什么以及如何解决它/它们:

构建 2 个类。第一堂课是“书”课。book 类有 4 个变量,它们都是私有的。第一个是checkOut,它是一个布尔值,初始化为false,title是一个由输入变量初始化的字符串,author是一个字符串,由一个输入变量初始化,pages是一个整数,也由一个输入变量。这个类还将有 4 个与 is 关联的函数。第一个将返回变量checkedOut。第二个将更改checkedOut 的值。如果该值设置为 true,那么它将更改为 false,反之亦然。第三个函数将返回页数,最后一个函数将返回标题。打印书籍对象时,其格式为“标题作者页面已签出”。

第二类将被称为库。初始化库时,它将创建一个名为集合的空字典。库类将有 2 个功能。第一个函数将被称为 addBook,它将接受 3 个输入变量,标题、作者和页面。在此函数中,您将创建一个书籍对象,然后将其添加到以标题为键的字典中。第二个函数将接收一本书的标题,在字典中找到这本书并调用改变已签出状态的 books 函数。最后,当打印出图书馆对象时,它将在单独的行上打印出图书馆中的每本书。

最后,库类将在名为 main.py 的 python 程序中实现。

这是我的代码:

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.")

我在这里做的有什么问题?当我尝试创建对象并在 IDLE 中运行代码时,我不断收到错误,即未定义某个变量名。

4

2 回答 2

3

(0) 下次粘贴错误。

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) 仅此部分。

checkedOut没有定义。你在哪里看到签出?就在函数的正上方checked_Out。好的。简短的回答,添加self.

例子:

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

你真的不应该在那里做标题,作者的东西。它们成为类变量,而不是实例变量。有区别。

input(2)如果您仍在使用 2.x Python,请避免使用。

使用raw_input和摆脱str. 这样更安全。在 3.x 中,您可以使用输入,它始终是字符串(raw_input 将始终返回字符串)。这也造成了问题。

(3)你有把一切都搞定的坚韧。我通常很冷静,但有点糟糕。不要称它为checked_Out真正的不一致加上 Python 程序员更喜欢checked_out. checkedOut可以起名来checked_out和嘿嘿,可以冲突。不要将您的函数和变量命名如此相似。

于 2013-04-24T01:31:40.617 回答
0

单独创建类,单独获取输入。另外,您要提到每个变量都是实例变量,而不是在本地范围内:

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.")

然后,将其余部分添加到main函数中:

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)
于 2013-04-24T02:03:46.337 回答