所以我是Python的新手,现在我正在研究继承。我有一个名为LibraryItem的类名,里面装满了书和杂志,我已经使用它手动添加了它们
def new_book(self):
self.libraryitems.append(Book("The Fault in Our Stars", "Fiction", "asdasd", "John Green", "012012", "D12"))
但是当我键入1来打印所有书籍时,它在CMD中不返回任何内容,而在IDLE中则无效。我的代码有什么问题吗?
当我想使用1显示库项目时:
def show_libraryitems(self, libraryitems=None):
if not libraryitems:
libraryitems = self.Catalog.libraryitems
for Book in libraryitems:
print("{0}: {1}\n{2}".format(libraryitem.title, libraryitem.subject))
这是完整的代码
这是菜单类
import sys
from LibraryItem import LibraryItem, Catalog, Book, Magazine, DVD
class Menu:
'''Display a menu and respond to choices when run.'''
def __init__(self):
self.Catalog = Catalog()
self.choices = {
"1": self.show_libraryitems,
"2": self.search_libraryitems,
"3": self.quit
}
def display_menu(self):
print("""
Menu
1. Show all File
2. Search Library
3. Quit
""")
def run(self):
'''Display the menu and respond to choices.'''
while True:
self.display_menu()
choice = input("Enter an option: ")
action = self.choices.get(choice)
if action:
action()
else:
print("{0} is not a valid choice".format(choice))
def show_libraryitems(self, libraryitems=None):
if not libraryitems:
libraryitems = self.Catalog.libraryitems
for libraryitem in libraryitems:
print("{0}: {1}\n{2}".format(libraryitem.title, libraryitem.subject, libraryitem.contributor))
def search_libraryitems(self):
filter = input("Search for: ")
libraryitems = self.Catalog.search(filter)
self.show_libraryitems(libraryitems)
def quit(self):
print("Thank you for using LibraryItem")
sys.exit(0)
if __name__ == "__main__":
Menu().run()
这是LibraryItem类
import datetime
last_id = 0
class LibraryItem:
def __init__(self, title, subject, contributor=''):
self.title = title
self.subject = subject
self.contributor = contributor
def match(self, filter):
return filter in self.title or filter in self.subject
class Book(LibraryItem):
def __init__(self, title, subject, contributor, author, isbn, dds_number=''):
super().__init__(title, subject, contributor)
self.author = author
self.isbn = isbn
self.dds_number = dds_number
class Magazine(LibraryItem):
def __init__(self, title, subject, contributor, volume, issue=''):
super().__init__(title, subject, contributor)
self.volume = volume
self.issue = issue
class DVD(LibraryItem):
def __init__(self, title, subject, contributor, actors, director, genre=''):
super().__init__(title, subject, contributor)
self.director = director
self.genre = genre
class Catalog:
def __init__(self):
self.libraryitems = []
def new_book(self):
self.libraryitems.append(Book("The Fault in Our Stars", "Fiction", "asdasd", "John Green", "012012", "D12"))
def new_magazine(self):
self.libraryitems.append(Magazine("VOGUE", "lifestyle", "asdasd", "name", "asd"))
def search(self, filter):
return [libraryitem for libraryitem in self.libraryitems if
libraryitem.match(filter)]
答案 0 :(得分:1)
在Python 2上,您应该使用raw_input
,而不是input
。自己尝试看看:
$ python2
Python 2.7.14 (default, Oct 31 2017, 21:12:13)
[GCC 6.4.0] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = input('blah: ')
blah: 1
>>> x
1
>>> type(x)
<type 'int'>
在Python 2上,input(...)
接受您输入的字符串,(不幸的是)将其通过eval(...)
传递,因此您将返回int
而不是字符串。您可以自己测试一下,看看。
在Python 3上,input(...)
与Python 2的raw_input(...)
相同。在Python 2上,几乎不要使用input()
;这被认为是Python 3中已纠正的错误。
也就是说,我很困惑,因为您的其余代码看起来像Python3。例如,不带参数的super()
是Python 3语法。