您好,我正在做一个我需要的项目:
- 在你的python脚本中定义一个“Textbook”类。
- 为您拥有的5本教科书创建教科书课程列表。
- 生成所有五本教科书的摘要,如末尾所示。
我相信我已经掌握了所有必要的信息,但在运行以下脚本时出现此错误:
总结()缺少1个必需的位置参数:'text'
我做错了什么?我对Python / Anaconda这么糟糕(不管有什么区别) 脚本如下:
class Textbook:
def __init__(self,name):
self.name=name
def title(self,text):
self.title=text
def author(self,text):
self.author=text
def publisher(self,text):
self.publisher=text
def year(self,text):
self.year=text
def course(self,text):
self.course=text
def semester(self,text):
self.semester=text
def summarize(self,text):
self.summarize=text
my_textbooks=[]
mybook1 = Textbook('1')
mybook1.title="Introduction to Python Class"
mybook1.author="Inseok Song"
mybook1.publisher="UGA"
mybook1.year=2016
mybook1.course="PHYS2001"
mybook1.semester="2016Fa"
my_textbooks.append( mybook1 )
mybook2 = Textbook('2')
mybook2.title="Calculus III"
mybook2.author="LaFollette"
mybook2.publisher="Blackwell"
mybook2.year=2006
mybook2.course="MATH 2270"
mybook2.semester="2017Fa"
my_textbooks.append( mybook2 )
mybook3 = Textbook('3')
mybook3.title="Why Be Good"
mybook3.author="John Hardwin"
mybook3.publisher="Corner Mill"
mybook3.year=2016
mybook3.course="PHIL 3400"
mybook3.semester="2017Fa"
my_textbooks.append( mybook3 )
mybook4 = Textbook('4')
mybook4.title="Astronomy for Beginners"
mybook4.author="J.P Callault"
mybook4.publisher="UGA"
mybook4.year=2017
mybook4.course="ASTR 1110"
mybook4.semester="2017Fa"
my_textbooks.append( mybook4 )
mybook5 = Textbook('5')
mybook5.title="Integrated Chinese"
mybook5.author="Chuan-Har Liu"
mybook5.publisher="UGA"
mybook5.year=2016
mybook5.course="CHNS 2001"
mybook5.semester="2017Fa"
my_textbooks.append( mybook5 )
for book in my_textbooks:
book.summarize()
答案 0 :(得分:0)
Python不是Java。你不需要为每个财产设置setter函数。
使用__init__
方法初始化您的对象:
class Textbook:
def __init__(self, title, author, publisher, year,
course, semester):
self.title = title
self.author = author
self.publisher = publisher
self.year = year
self.course = course
self.semester = semester
def summarize(self):
s = '{:40s} {:15s} {:15s} {:4d} {:8s} {:7s}'
return s.format(self.title, self.author,
self.publisher, self.year,
self.course, self.semester)
my_textbooks=[]
mybook1 = Textbook("Introduction to Python Class", "Inseok Song",
"UGA", 2016, "PHYS2001", "2016fa")
my_textbooks.append(mybook1)
# You can also create a textbook and add it to the list at
# the same time.
my_textbooks.append(Textbook("Calculus III", "LaFollette",
"Blackwell", 2006, "MATH2270", "2017fa"))
for book in my_textbooks:
# Print whatever you like as a summary
print(book.summarize())