0

My python readability program is not giving me the correct grade level for all of the checkers.

specifically: "In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since."

should spit out a grade level of 7, but gives me a grade level of 9. All of the other checkers work though! What's going on with this? IS it the way my floats are being rounded? Or am I missing something?

from cs50 import get_string

text = get_string("Text: ")
letters = 0
words = 0
sentences = 0
for i in range(len(text)):
    if (text[i] >= 'A' and text[i] <= 'z'):
        letters += 1
    if (text[i] == ' ' or text[i] == '\0'):
        words += 1
    if (text[i] == '.' or text[i] == '?' or text[i] == '!'):
        sentences += 1
W = 100 / words
L = letters * W
S = sentences * W
grade = round(0.0588 * L - 0.296 * S - 15.8)
if (grade < 1):
    print("Before Grade 1")
elif (grade > 16):
    print("Grade 16+")
else: 
    print("Grade ", end = "")
    print(grade)

1 Answer 1

0

Initialize your word to 1 not 0 and that should do the trick. because if the your text ends for example like "One fish, Two fish", since your algorithm counts words by spaces. check the example here there are only 3 spaces but we have 4 words, your program reads 3 words wherein there is really 4 words.

1
  • makes sense! I was actually trying to append a space to the end of each text element to combat that, but your solution is much better! Thanks!
    – b-hintz
    Commented Apr 26, 2020 at 23:19

You must log in to answer this question.

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