I have this thing right now, but the first part is something iffy with because the answer should be 12 because my file have 12 chapters , but i get 2726 as the answer.
I also need to make a forth function called:
analyze_book(filename, chapter_delimiter, word)
where the task is to use the three other functions to take in a word, calculate how many times that word is in each chapter of the book, and then plot the resaults in a graf where the x-axes is the chapternumber and the y-axes is the amount of times that word is in that chapter.
the graf is also going to look like this:
- Title of the graf is: Amount of word shows up per chapter in filename
- Title x-axis: Chapter
- Title y-axis: Amount of word
- x-axis goes from 1 to amount of chapters
- y-axis goes from 0 to highest amount og times word +3
As said at the top, def get_chapter is a litte iffy somewhere and i dont know why
And i also lack the last function and i have no idea where to go from here.
I would appreciate any help i can get
>Solution :
Your function of count the chapters could be implemented like this:
def get_chapters(filename,chapter_delimiter):
lines = None
with open(filename,'r') as f:
lines = f.readlines()
num_chapters = len(lines.split(chapter_delimiter))
return num_chapters