我的“总结”命令有什么问题?

2024-06-01 07:13:45 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,我正在做一个项目,我需要:

-在python脚本中定义一个“教科书”类。你知道吗

-为你拥有的5本教科书创建一个教科书类列表。你知道吗

-总结所有五本教科书,如结尾所示。你知道吗

我相信我已经记下了所有必要的信息,但在运行下面的脚本时出现此错误:

summary()缺少1个必需的位置参数:“text”

我做错了什么?我太不擅长Python/Python了(不管有什么不同) 脚本如下:

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

Tags: textselftitlemydefyearpublisherauthor
1条回答
网友
1楼 · 发布于 2024-06-01 07:13:45

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

相关问题 更多 >