我的 "summarize" 命令有什么问题?
What is wrong with my "summarize" command?
您好,我正在做一个项目,我需要:
-在您的 python 脚本中定义一个 "Textbook" class。
-为您拥有的 5 种教科书创建一个教科书列表 class。
-生成所有五本教科书的摘要,如末尾所示。
我相信我已经记下了所有必要的信息,但是当 运行 下面的脚本时出现此错误:
summarize() 缺少 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()
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())
您好,我正在做一个项目,我需要:
-在您的 python 脚本中定义一个 "Textbook" class。
-为您拥有的 5 种教科书创建一个教科书列表 class。
-生成所有五本教科书的摘要,如末尾所示。
我相信我已经记下了所有必要的信息,但是当 运行 下面的脚本时出现此错误:
summarize() 缺少 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()
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())