2013-04-24 55 views
0

作为一个任务,我试图创建两个类:一类,Book,查看是否一书已签出和返回标题,作者和页码一本书(这些是输入变量)和另一个名为Library的类将标题 - 作者对添加到字典中,并查看是否某本书被检出。每次我尝试运行时都会收到一条错误消息。我如何解决这个奇怪的错误?NameError:全局名称“集合”没有定义

这里是我的代码:

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 
+0

在你的'__init__'集合中是一个局部变量。你需要做自我。集合'为它是一个实例变量 – RedBaron 2013-04-24 04:50:39

+0

您需要在所有地方将'collection'更改为'self.collection'。 – 2013-04-24 04:51:26

回答

2

您在__init__定义collection作为一个局部变量:

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() 

下面是当我尝试运行它会发生什么

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 

他们混淆了直白book.title属性的另一种层。

而且,你不需要写.keys()if key in dictionary是首选语法:

if title in self.collection: 
1

添加self.collection在任何你所引用的集合。

1

类成员必须使用self.propertyName进行访问。您的代码应该是这样的:

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

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

    .... 
0

在你Collection类,你必须使用内部collection

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

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

而且最后一行看起来可疑。您的意思是这样的:

self.collection[book.title] = book 
0

收集是一个局部变量

尝试与self.collection的功能,这应该解决您的问题引用它。

0

你应该告诉蟒蛇是collection属于图书馆的实例,myLib

修改库类的说:self.collection无处不在,你现在有collection

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

希望这有助于!

相关问题