-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadability.py
More file actions
55 lines (41 loc) · 1.21 KB
/
Copy pathreadability.py
File metadata and controls
55 lines (41 loc) · 1.21 KB
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
from cs50 import get_string
def main():
text = get_string("Text: ") # Ask user for a piece of text
letters = count_letters(text)
words = count_words(text)
sentences = count_sentences(text)
CL_Index(letters, words, sentences) # ColemanLiauIndex calculation
def count_letters(text):
count = 0
for i in text:
if str.isalpha(i) == True:
count += 1
return count
def count_words(text):
count = 1 # One because of the last word (assumes no space at the end)
for i in text:
if str.isspace(i) == True:
count += 1
return count
def count_sentences(text):
count = 0
for i in text:
if ord(i) == 33: # If text has "!"
count += 1
elif ord(i) == 46: # If text has "."
count += 1
elif ord(i) == 63: # If text has "?"
count += 1
return count
def CL_Index(letters, words, sentences):
L = letters / words * 100
S = sentences / words * 100
# CL_Index formula
index = 0.0588 * L - 0.296 * S - 15.8
if index < 1:
return print("Before Grade 1")
elif index >= 16:
return print("Grade 16+")
else:
return print("Grade", round(index))
main()