Search code examples
pythonsummarize

What is wrong with my "summarize" command?


Hello, I am doing a project in which I need to:

-define a "Textbook" class in your python script.

-create a list of Textbook class for 5 textbooks you possess.

-produce a summary of all five Textbooks as shown at the end.

I believe that I have all the necessary information down but I get this error when running the script below:

summarize() missing 1 required positional argument: 'text'

What am I doing wrong? I am so bad at Python/Anaconda (whatever the difference is) Script is below:

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

Solution

  • Python is not Java. You don't need setter functions for every property.

    Use the __init__ method to initialize your object:

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