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)