如何获取我的__str __()函数的返回值以在我的main中打印?

时间:2013-04-24 21:25:18

标签: python class

我需要为一个程序创建两个类,该程序接受书籍,标题,作者,页数的输入变量,并查看是否已检出,然后以下列格式将书籍添加到字典{{1 }}。然后我需要以这种格式在主函数中打印我的信息:“”title author pages checkedOut。“我基本上需要在我的类中打印出dictionary[title] = author函数的返回值。这是我的代码:

__str__()

在类库中我的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 def __str__(self): return ("Title: " + self.title + "Author: " + self.author + "Pages: " + self.pages + "Checked Out Status: " + self.checkedOut) 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: title.change_value_of_checkedOut() else: print("This book is not in the collection.") def __str__(self): for myBook in self.collection[myBook]: self.collection[myBook] self.collection[myBook] = self.author # I want to print this return value in my main return ("Title: " + self.title + "Author: " + self.author + "Pages: " + self.pages + "Checked Out Status: " + self.checkedOut) 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) myLib2 = Library() myLib3 = myLib2.__str__() print(myLib3) main() 函数中的for循环似乎有问题(我的想法是我希望循环遍历集合中的所有书)但是我不确定是什么问题是。以下是我收到的错误消息:

__str__(self)

2 个答案:

答案 0 :(得分:3)

你正在循环字典的键,你应该尝试在循环语句中传入一个键:

def __str__(self):
    for myBook in self.collection:

目前还不清楚你在尝试用这个循环做什么。您正在访问self.collection[myBook]而未对返回值执行任何操作,然后您将{/ 1>}替换为<{1}}(self.author上不存在的值)仍然是库),然后你返回一些与self结果完全不同的东西。

你想在这里创建一本图书馆书籍列表:

__str__()

现在,您将返回以逗号分隔的列表,其中列出了您馆藏中的所有标题和作者。

那里有三件事:

  • 使用生成器表达式,我们遍历字典.items() method;这会为字典中的所有内容生成def __str__(self): return ', '.join('{} - {}'.format(title, author) for title, author in self.collection.items()) 对。
  • 每个键和值都会传递给str.format() method,使用字符串作为模板,在(key, value)字典中存储的标题和作者对之间添加-短划线。
  • 整个循环传递给str.join() method;此方法从给定的序列中获取所有字符串,并将它们连接在一起,并在它们之间使用self.collection逗号。

接下来,您将创建一个库,然后打印', '方法的输出;没有必要明确地调用它,__str__()为你做这件事。不要创建新库,只需打印已有的库:

print()

答案 1 :(得分:0)

我想你要改变

def __str__(self):
        for myBook in self.collection[myBook]:
            self.collection[myBook]
            self.collection[myBook] = self.author

def __str__(self):
        for myBook in self.collection.keys():
            self.collection[myBook]
            self.collection[myBook] = self.author