0

Probably its something totally stupid but I'm stuck and could need help from the community. In CS50 2021 Week 6 we have to do the readability calculation we already did in C.

The task: "Write [...] a program that first asks the user to type in some text, and then outputs the grade level for the text, according to the Coleman-Liau formula, exactly as you did in Problem Set 2, except that your program this time should be written in Python. Recall that the Coleman-Liau index is computed as 0.0588 * L - 0.296 * S - 15.8, where L is the average number of letters per 100 words in the text, and S is the average number of sentences per 100 words in the text."

It should look like this:

Text: Congratulations! Today is your day. You're off to Great Places! You're off and away!

Grade 3

Unfortunately, my code is throwing a Grade 11 instead and I really don't see the error anymore.

What's wrong?

from cs50 import get_string
import re

# ask user for input
text = get_string("Text: ")

# get informations out of text
number_letters = 0
for i in range(len(text)):
    if text[i].isalpha():
        number_letters += 1

number_words = len(text.split())
number_sentences = len(re.split(r'[.!?]', text))-1

# calculate index
L = number_letters * (100 / number_words)
S = number_sentences * (100 / number_words)

index = round(0.0588 * L - 0.296 * S - 15.8)
index2 = 0.0588 * (100 * float(number_letters) / float(number_words)) - 0.296 * (100 * float(number_sentences) / float(number_words)) - 15.8
print("index2:", index2)

#print("Index", index)
print("Letters:", number_letters)
print("Words:", number_words)
print("Sentence:", number_sentences)

# print grades
if index >= 16:
    grade = "Grade 16+"
elif index <= 1:
    grade = "Before Grade 1"
else:
    grade = f"Grade {index}"

print(grade)
  • 2
    Are you sure that 3 is the expected grade for the `Congratulations! Today is your day. You're off to Great Places!` input ? I have put this text in the first Google result for "Coleman-Liau index online" and it is telling me the index is `13.6`, which is on par with your computation. I double-checked your code and it looks fine. – Lenormju Jun 18 '21 at 19:42
  • Yes. It's strange. Here are some of the inputs: Input: "In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since...." <-- Expected: Grade 7. Got: Grade 14. Input: "There are more things in Heaven and Earth, Horatio, than are dreamt of in your philosophy...." <-- Expected: Grade 9. Got: Grade 15. – ThilotoSanchez Jun 19 '21 at 11:23
  • This might help https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ ! – JeffUK Jun 19 '21 at 11:59

2 Answers2

3

The number_letters calculation is overstated by using len(text). That is the total number of characters in the input, not (as per the spec):

... a letter is any lowercase character from a to z or any uppercase character from A to Z

Iteration is your friend.

DinoCoderSaurus
  • 6,110
  • 2
  • 10
  • 15
1

Finally found the solution. I updated the solution above. The Coleman-Liau formula isn't taking the length of the text but really just the number of letters without any spaces and non-alphabetical characters. By including a simple for-if loop, the results are looking completely different.

  • fyi you can simplify your revised loop by summing an `isalpha` generator: `number_letters = sum(char.isalpha() for char in text)` – tdy Jun 20 '21 at 08:20