Skip to content
Advertisement

Coleman Liau index formula just will not work

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) :/

from cs50 import get_string

def count_sentences(text):
    index = 0

    length = len(text)

    amount_of_sentences = 0
    while index < length:
        if text[index] == '.' or text[index] == '!' or text[index] == '?':
            amount_of_sentences += 1

        index += 1

    return amount_of_sentences
def count_words(text):

    index = 0

    length = len(text)

    amount_of_words = 0
    while index < length:
        if text[index] == ' ':
            amount_of_words += 1

        index += 1
    amount_of_words += 1
    return amount_of_words

def count_letters(text):

    index = 0

    length = len(text)

    amount_of_letters = 0

    while index < length:
        if (text[index].isalpha() and text[index] == ' ') == False:
            amount_of_letters += 1

        index += 1


    return amount_of_letters



def algorithm(text):
    words = count_words(text)
    letters = count_letters(text)
    sentences = count_sentences(text)
    print(words, letters, sentences)
    return 0.0588 * (letters / words * 100) - 0.296 * (sentences / words * 100) - 15.8
text = get_string("Text: ")

grade_level = algorithm(text)
print(grade_level)
if grade_level < 1:
    print("Before Grade 1")

elif grade_level >= 16:
    print("Grade 16+")

else:
    print("Grade", round(grade_level))

Advertisement

Answer

The problem was my count_letters() function :/ . The and text[index] == ' ') == False was the problem

def count_letters(text):

    index = 0

    length = len(text)

    amount_of_letters = 0

    while index < length:
        if text[index].isalpha():
            amount_of_letters += 1

        index += 1


    return amount_of_letters```
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement