0
from cs50 import get_string

text = get_string("Text: ") 
letters = sentences = i = words = 0
length = len(text)
while i < length:
    if text[i].isalpha():
        letters += 1
        //print(letters)
    if (i == 0 and text[i] != " ") or (i != length and text[i] == " " and text[i + 1] != " "):
        words += 1
        //print(words)
    if text[i] in ["?" , "!" , "."]:
        sentences += 1
        //print(sentences)
    i += 1   
    
L = (letters / words) * 100
S = (sentences / words) * 100

grade = round(0.0588 * L - 0.0296 * S - 15.8)
//print(f"0.0588 * {L} - 0.296 * {S} - 15.8")
//print(grade)

if grade < 1:
    print("Before Grade 1")
elif grade >= 16:
    print("Grade 16+")
else:
    print(f"Grade {grade}")

I'm working on a task of readability in python, but the answer is too high. for example, for input;

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

Output : Grade 11

it should print "Grade 3". I tried to check each line by printing one by one, and it worked fine and it calculates correctly until I checked this line below. the output gets higher than i expected. what did i do wrong?

grade = round(0.0588 * L - 0.0296 * S - 15.8)

1 Answer 1

1
grade = round(0.0588 * L - 0.0296 * S - 15.8)

There's a definite problem with that formula. Maybe you should check the code against the actual formula to see what's different. ;-)

1
  • ahh yes, a small but fatal mistake! thank you very much! haha Commented Dec 2, 2020 at 9:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .