0

I managed to translate readability from C to python, except it is printing one grade lower than what it is supposed to when testing.

What am I missing here?

"""
1) Ask user for phrase
2) Count number of letters, words, and sentences
3) Calculate L and S
4) Use formula
5) Print grade based on formula
"""
#Ask user for phrase
phrase = input("Phrase: \n")

#Declare variables
index = 0
i = 0
countLetters = 0
countWords = 0
countSentences = 0

#Check that phrase is an alpha-numeric phrase
if phrase[0].isalpha:
    countWords += 1
else:
    exit()

#Start counting words, letters, and sentences
if (phrase):
    for i in range(len(phrase)):
        
        #Letters
        if phrase[i].isalpha():
            countLetters += 1

        #Words
        if (i == 0 and phrase[i] !=" ") or (i != len(phrase) and phrase[i] == " " and phrase[i + 1] != " "):
            countWords += 1

        #Sentences
        if phrase[i] == "." or phrase[i] == "?" or phrase[i] == "!":
            countSentences += 1
            i += 1
    
#Calculate L and S
L = (countLetters/countWords)*100
S = (countSentences/countWords)*100

index = round(0.0588 * L - 0.296 * S - 15.8)

if index < 1:
    print("Before Grade 1")

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

else:
    print("Grade {}".format(index))
0

Have you checked that the code is counting the words sentences and letters correctly? It seems that the word count is off by 1.

1
  • Fixed the word count to only make sure that the first word is an alpha-numeric value The program worked in C (used if statements as well), but does not seem to be translating well in python Dec 2 '20 at 14:20

You must log in to answer this question.

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