I have my code for implementing a program that calculates the grade level needed to comprehend some text (You might know CS50)
I try to implement a class object even though it may not be necessary. But I'm practicing with it. Anyway, could you maybe have a look and tell me where I am wrong and it can't work like that? Can you maybe give me some improvements?
Thank you very much for your help.
Ah, and the code goes on with actually doing some iteration to actually filter the text from input (for example read the spaces and so on). But first it's about this issue.
When I add values in Read() --> TypeError: init() should return None, not 'function' Without values I get --> TypeError: Read.init() missing 3 required positional arguments: 'text_num', 'space_count', and 'marks_count'
# import libary
import cs50
def main():
read = Read()
aver_letters_words = round(read.text_num / read.space_count * 100)
aver_sent_words = round(read.marks_count / read.space_count * 100)
# Adding the variables into the Coleman-Liau index formula
index = 0.0588 * aver_letters_words - 0.296 * aver_sent_words - 15.8
#Checking the limits of the grade level via if-statement
if index < 1:
print("Before Grade 1")
elif index >= 16:
print("Grade 16 ")
else:
print("Grade", index)
class Read():
def __init__(self, text_num, space_count, marks_count):
self.text_num = text_num
self.space_count = space_count
self.marks_count = marks_count
#read = Read()
#print(read.text_num)
return main
main()
CodePudding user response:
I feel like the python interpreter is rather precise with the error description given here. Also, I believe you have two independent errors here.
Firstly: init() should return None, not 'function'
. You have a return main
at the end of your class definition, which returns a function main
. I don't quite understand what are you trying to achieve, but get rid of this line to remove the first problem.
As for the second bug, TypeError: Read.init() missing 3 required positional arguments: 'text_num', 'space_count', and 'marks_count'
. You have defined the Read().__init__
method with three arguments, but while creating the object in line 6, you try to create the Read()
object without any of them. Either specify some default values in the __init__
or provide the positional arguments in line 6.
Finally, since you are using the Read
class to store data only, I would suggest using a dict
or a dataclass
(Python 3.7 ) instead.
For the latter, see: https://realpython.com/python-data-classes/.
If I understand your intents correctly, a python solution to your problem might look something like below:
class Text():
def __init__(self, text_num, space_count, marks_count):
self.text_num = text_num
self.space_count = space_count
self.marks_count = marks_count
def evaluate():
aver_letters_words = round(read.text_num / read.space_count * 100)
index = 0.0588 * aver_letters_words - 0.296 * aver_sent_words - 15.8
if index < 1:
print("Before Grade 1")
elif index >= 16:
print("Grade 16 ")
else:
print("Grade", index)
if __name__ == '__main__':
t = Text(12, 1, 1) # input here manually for text 'Hello world!'
t.evaluate() # this prints the output
CodePudding user response:
You should make main a part of Read if you must use a class. Though classes aren't a means to do actions, Read should be your function and that's it.