Python 名称错误:全局名称';收集';没有定义

Python 名称错误:全局名称';收集';没有定义,python,class,Python,Class,作为一项任务,我尝试创建两个类:一个类,Book,查看一本书是否已签出并返回书中的标题、作者和页码(这些是输入变量),另一个类称为Library,将标题-作者对添加到词典中,查看某本特定的书是否已签出。每次尝试运行时,我都会收到一条错误消息。如何修复这个奇怪的错误 这是我的密码: class Book: def __init__(self, title, author, pages): self.title = title self.author = au

作为一项任务,我尝试创建两个类:一个类,
Book
,查看一本书是否已签出并返回书中的标题、作者和页码(这些是输入变量),另一个类称为
Library
,将标题-作者对添加到词典中,查看某本特定的书是否已签出。每次尝试运行时,我都会收到一条错误消息。如何修复这个奇怪的错误

这是我的密码:

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()
下面是我尝试运行它时发生的情况:

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
输入书的标题。基督山伯爵
输入书的作者。大仲马
输入书本中的页数。1250
回溯(最近一次呼叫最后一次):
文件“C:/Python33/Class Programs/book\u library\u classes.py”,第56行,在
main()
文件“C:/Python33/Class Programs/book\u library\u classes.py”,第54行,在main中
myLib.addExistingBook(myBook)
文件“C:/Python33/Class Programs/book_library_classes.py”,第36行,在addExistingBook中
集合[图书.标题]=图书.作者
NameError:未定义全局名称“集合”

您在
\uuuu init\uuu
中将
集合定义为局部变量:

def __init__(self):
    collection = {}
但这并不能神奇地使它成为一个实例变量。您必须明确地这样做:

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

    def addExistingBook(self, book):
        self.collection[book.title] = book.author
此外,我不会使用这样的方法:

def return_title(self):
    print(self.title)
    return self.title
class Library:
    def __init__(self):
        self.collection = {}

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

     ....
它们只是简单的
book.title
属性上的另一层模糊

此外,您不需要编写
.keys()
<代码>如果字典中的键
是首选语法:

if title in self.collection:

您在
\uuuu init\uuuu
中将
集合定义为局部变量:

def __init__(self):
    collection = {}
但这并不能神奇地使它成为一个实例变量。您必须明确地这样做:

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

    def addExistingBook(self, book):
        self.collection[book.title] = book.author
此外,我不会使用这样的方法:

def return_title(self):
    print(self.title)
    return self.title
class Library:
    def __init__(self):
        self.collection = {}

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

     ....
它们只是简单的
book.title
属性上的另一层模糊

此外,您不需要编写
.keys()
<代码>如果字典中的键
是首选语法:

if title in self.collection:

在引用集合的位置添加
self.collection

在引用集合的任何位置添加
self.collection

必须使用
self.propertyName
访问类成员。您的代码应该如下所示:

def return_title(self):
    print(self.title)
    return self.title
class Library:
    def __init__(self):
        self.collection = {}

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

     ....

必须使用
self.propertyName
访问类成员。您的代码应该如下所示:

def return_title(self):
    print(self.title)
    return self.title
class Library:
    def __init__(self):
        self.collection = {}

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

     ....

集合
类中,您必须使用内部
集合

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

    def addExistingBook(self, book):
        self.collection[book.title] = book.author
最后一行看起来也很可疑。你是说:

self.collection[book.title] = book

集合
类中,您必须使用内部
集合

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

    def addExistingBook(self, book):
        self.collection[book.title] = book.author
最后一行看起来也很可疑。你是说:

self.collection[book.title] = book

集合是一个局部变量


尝试使用self.collection在函数中引用它,这将解决您的问题。

collection是一个局部变量


尝试使用self.collection在函数中引用它,这会解决您的问题。

您应该告诉python,
collection
属于库实例,
myLib

将Library类修改为:
self.collection
当前所有的
collection
i、 e


希望这有帮助

您应该告诉python集合属于库实例,
myLib

将Library类修改为:
self.collection
当前所有的
collection
i、 e


希望这有帮助

\uuuu init\uuu
集合中,是一个局部变量。您需要执行
self.collection
以使其成为一个实例变量您需要在所有位置将
collection
更改为
self.collection
。在
中,collection是一个局部变量。您需要执行
self.collection
以使其成为实例变量。您需要在所有位置将
collection
更改为
self.collection