Book.No_Pages() missing 2 required positional arguments: 'Words' and 'Font_size'
the error comes in line 14
The code is:-
class Book():
def __init__ (b1,Font_size=12,Words=300):
b1.Words = Words
pass
b1.Font_size = Font_size
pass
def No_Pages(b1,Words,Font_size):
return b1.Words/b1.Font_size
cyn_Book = Book(300,12)
print(cyn_Book.Font_size)
print(cyn_Book.Words)
print(cyn_Book.No_Pages())
I actually tried this by another approach like adding pass after the Font_size
and Words
and thought that it could return words 300
,Font_size 12
and pages
being 15
CodePudding user response:
So No_pages
isn't using those parameters (Words
and Font_size
) - it's getting them from the b1
parameter (which would conventionally be called self
, btw.) Essentially you're handing it an entire container, b1
, and it's getting everything it needs from there - but you've also told it it should be explicitly handed Words
and Font_size
, which it doesn't need. Because you're not passing them, it's failing.
You can fix this by changing the def No_Pages(b1,Words,Font_size):
to def No_Pages(b1):
By the way, what did you think pass
would do? That doesn't actually do anything, it's just used as a placeholder if you have a block of code you haven't written yet (and need to have something there.) For instance, you might do:
if some_condition:
pass
because you haven't worked out the statements you need yet, but omitting the pass statement would make python fail because it expects something to be there.
CodePudding user response:
Instead of referring to the instance using b1
, you need to add the self
argument to class methods, for the sake of convention. For eg., your __init__
would then become
def __init__(self, Font_size=12, Words=300):
self.Words = Words
self.Font_size = Font_size
Moreover, your NoPages
function takes 3 parameters and you're providing only one, that is b1
. Hence the error Book.No_Pages() missing 2 required positional arguments: 'Words' and 'Font_size'
You may rewrite it as
def No_Pages(self):
return self.Words/self.Font_size
CodePudding user response:
Change your class definition to:
class Book:
def __init__ (self, font_size=12, words=300):
self.font_size = font_size
self.words = words
def no_pages(self):
return self.words / self.font_size
When you define a function like no_pages
you say what arguments it expects. If you define it like this:
def No_Pages(b1,Words,Font_size):
then it expects three arguments: b1
(the object itself, which is provided automatically when you invoke an instance method, and usually referred to as self
), Words
, and Font_size
. Since the function expects the Words
and Font_size
arguments, it will raise an error if you call it without them. You don't need those arguments to be part of this function at all, though, since self
already contains them.
Adding pass
doesn't do anything; you only use the pass
keyword in cases where a block of code is expected (e.g. in the body of an except
) but you don't actually want to execute anything.
You can simplify your class further if you use @dataclass
, which builds the constructor for you based on the class's attributes:
from dataclasses import dataclass
@dataclass
class Book:
font_size: int = 12
words: int = 300
def no_pages(self):
return self.words / self.font_size