I’m writing readability in python for CS50, and I’ve tried so many different Coleman liau index formulas but it still will not work no matter what I do. My letter, word and sentence counters are giving me the correct output.
I’ve been trying for hours to the point where I just copied and pasted an algorithm I found still doesn’t work… and translating from C doesn’t work (from my old code) :/
JavaScript
x
68
68
1
from cs50 import get_string
2
3
def count_sentences(text):
4
index = 0
5
6
length = len(text)
7
8
amount_of_sentences = 0
9
while index < length:
10
if text[index] == '.' or text[index] == '!' or text[index] == '?':
11
amount_of_sentences += 1
12
13
index += 1
14
15
return amount_of_sentences
16
def count_words(text):
17
18
index = 0
19
20
length = len(text)
21
22
amount_of_words = 0
23
while index < length:
24
if text[index] == ' ':
25
amount_of_words += 1
26
27
index += 1
28
amount_of_words += 1
29
return amount_of_words
30
31
def count_letters(text):
32
33
index = 0
34
35
length = len(text)
36
37
amount_of_letters = 0
38
39
while index < length:
40
if (text[index].isalpha() and text[index] == ' ') == False:
41
amount_of_letters += 1
42
43
index += 1
44
45
46
return amount_of_letters
47
48
49
50
def algorithm(text):
51
words = count_words(text)
52
letters = count_letters(text)
53
sentences = count_sentences(text)
54
print(words, letters, sentences)
55
return 0.0588 * (letters / words * 100) - 0.296 * (sentences / words * 100) - 15.8
56
text = get_string("Text: ")
57
58
grade_level = algorithm(text)
59
print(grade_level)
60
if grade_level < 1:
61
print("Before Grade 1")
62
63
elif grade_level >= 16:
64
print("Grade 16+")
65
66
else:
67
print("Grade", round(grade_level))
68
Advertisement
Answer
The problem was my count_letters() function :/ . The and text[index] == ' ') == False
was the problem
JavaScript
1
17
17
1
def count_letters(text):
2
3
index = 0
4
5
length = len(text)
6
7
amount_of_letters = 0
8
9
while index < length:
10
if text[index].isalpha():
11
amount_of_letters += 1
12
13
index += 1
14
15
16
return amount_of_letters```
17