-4

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()
Jakspigot
  • 3
  • 3
  • Please *format* your code properly. Edit your question, mark all the code, and press the `{}` button in the inline-editor toolbar. – Some programmer dude Nov 09 '17 at 05:01
  • Now when the code is easier to read, lets take a look at the `summarize` function. You define it to take an argument (`text`), but call it without passing any arguments. That's why you get the error. Which tells you exactly that. – Some programmer dude Nov 09 '17 at 05:06
  • So do I need to put something in the parentheses of book.summarize()? Or are you saying I need to define it differently at the top? – Jakspigot Nov 09 '17 at 05:14
  • What is the *purpose* of the `summarize` function? What is it supposed to *do*? Start with that. – Some programmer dude Nov 09 '17 at 05:15
  • the error is that, as you have defined your `summarize` function like this: `def summarize(self,text):` that is, it's taking one argument named `text` whenever it is called, and when you are calling it on line: ` book.summarize()`, here you are not supplying that parameter. – tkhurana96 Nov 09 '17 at 05:20
  • I think I get what you are saying but I'm not quite sure what I need to do - what should I change to supply the parameter? – Jakspigot Nov 09 '17 at 05:40
  • Is the function *supposed* to take an argument? Again, ***what is the function supposed to do?*** If you don't know that yourself, then how could we possibly help you? – Some programmer dude Nov 09 '17 at 06:32
  • Ohhhh, I finally looked it up and yeah, ok, I got rid of the parentheses and my code runs without an error now, but I don't get an answer that shows up, do I need to use a "return" function? – Jakspigot Nov 09 '17 at 13:24

1 Answers1

0

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())
Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • I never got around to thanking you for this so I'm thanking you now! This was super helpful and I finally figured it all out! I am so much more thankful than you think! – Jakspigot Nov 11 '17 at 22:21
  • I do have one last question however, if you can spare the time, my teacher wanted use to use the "summarize" command instead of the "print" command, but I am not sure how to use it exactly - I included his instructions below. title: 40 characters long author: 15 char long publisher: 15 char long year: 4 digits course: 8 char long semester: 7 char long – Jakspigot Nov 11 '17 at 22:22
  • Thank you so much once again Mr. Smith! – Jakspigot Nov 12 '17 at 06:35