duquesne9

readability.py

Oct 17th, 2020
740
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # read.py
  2. from cs50 import get_string
  3.  
  4.  
  5. def main():
  6.     # get string from user
  7.     text = get_string("Text: ")
  8.  
  9.     # get total letters, words, and sentences.
  10.     letter, word, sentence = counts(string=text)
  11.  
  12.     # calculate avg wors per 100 words
  13.     L = get_avg(letter, word)
  14.  
  15.     # calculate avg sentence per 100 words
  16.     S = get_avg(sentence, word)
  17.  
  18.     # find grade using:
  19.     # grade = 0.0588 * L - 0.296 * s - 15.8
  20.     grade = round((0.0588 * L) - (0.296 * S) - 15.8)
  21.  
  22.     print(print__grade(grade))
  23.  
  24.  
  25. def counts(string=''):
  26.     ''' return total letters, words, and sentences in text '''
  27.     letters = 0
  28.     words = 1
  29.     sentences = 0
  30.     for i in string:
  31.         if i.isalpha():
  32.             letters += 1
  33.         elif i.isspace():
  34.             words += 1
  35.         elif i in ["!", ".", "?"]:
  36.             sentences += 1
  37.     return letters, words, sentences
  38.  
  39.  
  40. def get_avg(item=0, outof=0):
  41.     ''' return avg item occurring in 100 outof '''
  42.     if outof < 100:
  43.         mult = 100 / outof
  44.         return item * mult
  45.     else:
  46.         divisor = outof / 100
  47.         return item // divisor
  48.  
  49.  
  50. def print__grade(score=0):
  51.     ''' return grade string '''
  52.     if score <= 0:
  53.         return "Before Grade 1"
  54.     elif score >= 16:
  55.         return "Grade 16+"
  56.     else:
  57.         return f"Grade {score}"
  58.  
  59.  
  60. if __name__ == '__main__':
  61.     main()
RAW Paste Data