Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

trying to create a function based on a bool

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. Title of the graf is: Amount of word shows up per chapter in filename
  2. Title x-axis: Chapter
  3. Title y-axis: Amount of word
  4. x-axis goes from 1 to amount of chapters
  5. 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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading