-
Notifications
You must be signed in to change notification settings - Fork 10
/
readability.py
61 lines (40 loc) · 1.24 KB
/
readability.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from cs50 import get_string
def main():
text = get_string("Text: ")
# Stores the number of letters, words and sentences
letters = count_letters(text)
words = count_words(text)
sentences = count_sentences(text)
# L = average number of letters per 100 words
L = (100 / words) * letters
# S = avarage number of sentences per 100 words
S = (100 / words) * sentences
index = round(0.0588 * L - 0.296 * S - 15.8)
if index < 1:
print("Before Grade 1\n")
elif index >= 16:
print("Grade 16+")
else:
print(f"Grade {index}")
def count_letters(text):
letters = 0
for i in range(0, len(text), 1):
if text[i].isalpha():
letters += 1
return letters
def count_words(text):
# First word has no space before
words = 1
for i in range(0, len(text), 1):
if text[i].isspace():
words += 1
return words
def count_sentences(text):
sentences = 0
# Consider sentence the phrase that ends with. ! or?
for i in range(0, len(text), 1):
if text[i] == "." or text[i] == "!" or text[i] == "?":
sentences += 1
return sentences
if __name__ == "__main__":
main()