This is my code and I think it is correct but, the grade for every paragraph is wrong because the index calculation is wrong. Although, the letters, words, and sentence counting is correct. So, where is the problem? someone help me, please?
from cs50 import get_string
import math
# Loop to count the letters
letters = 0
words = 0
sentences = 0
# Prompt user for some text.
text = get_string("Text: ")
# Loop to count the letters(s) of a paragraph
def count_letters():
global letters
for i in range(len(text)):
if text[i].lower() or text[i].upper():
letters += 1
# pass
print(letters)
return letters
count_letters()
# Loop to count the words of the paragraph.
def count_words():
global words
for i in range(len(text)):
if text[i].isspace():
words += 1
# TODO
if text[i].isspace() and text[i + 1].isspace():
words -= 1
print(words + 1)
return words + 1
count_words()
# Loop to count sentences of the paragraph.
def count_sentences():
global sentences
for i in range(len(text)):
if text[i] == "." or text[i] == "!" or text[i] == "?":
sentences += 1
print(sentences)
return sentences
count_sentences()
# Calc the index of the grades
def indexOfGrade():
global letters
global words
global sentences
l = letters / words * 100
s = sentences / words * 100
index = round(0.0588 * l - 0.296 * s - 15.8)
print(letters / words)
print(l)
print(s)
print(index)
# grades
if index >= 16:
print("Grade 16+")
elif index < 1:
print("Before Grade 1")
else:
print(f"Grade {index}")
indexOfGrade()
Yahooo, after a lot of time I figured out your mistake.
def count_words():
global words
for i in range(len(text)):
if text[i].isspace():
words += 1
# TODO
if text[i].isspace() and text[i + 1].isspace():
words -= 1
print(words + 1)
return words + 1 #This part is wrong
The last line, which is return will just return the value but will not change the value of the variable words, therefore, you need to correct it as follows
def count_words():
global words
for i in range(len(text)):
if text[i].isspace():
words += 1
# TODO
if text[i].isspace() and text[i + 1].isspace():
words -= 1
print(words + 1)
words += 1
return words
Also the letters, words and sentences variables may optionally be float and not int and while calculating the index, python will omit the remaining decimal part, therefore the round may not work.
Also, I have performed check50 on my device and all the results were green with (correct)
Also, if text[i].isspace() and text[i + 1].isspace():
was wrong you need to completely delete that part.
Therefore here is the final answer with the required changes.
from cs50 import get_string
import math
# Loop to count the letters
letters = float(0)
words = float(0)
sentences = float(0)
# Prompt user for some text.
text = get_string("Text: ")
# Loop to count the letters(s) of a paragraph
def count_letters():
global letters
for i in range(len(text)):
if (text[i] >= 'a' and text[i] <= 'z') or (text[i] >= 'A' and text[i] <= 'Z'):
letters += 1
# pass
# # print(letters)
return letters
count_letters()
# Loop to count the words of the paragraph.
def count_words():
global words
for i in range(len(text)):
if text[i].isspace():
words += 1
# # TODO
# if text[i].isspace() and text[i + 1].isspace():
# pass
# # print(words + 1)
words += 1
return words + 1
count_words()
# Loop to count sentences of the paragraph.
def count_sentences():
global sentences
for i in range(len(text)):
if text[i] == "." or text[i] == "!" or text[i] == "?":
sentences += 1
# # print(sentences)
return sentences
count_sentences()
# Calc the index of the grades
def indexOfGrade():
global letters
global words
global sentences
# print(letters)
# print(words)
# print(sentences)
l = 100 * letters / words
# print(l)
s = sentences / words * 100
# print(s)
index = round(0.0588 * l - 0.296 * s - 15.8)
# # print(letters / words)
# print(0.0588 * l - 0.296 * s - 15.8)
# print(l)
# print(s)
# print(index)
# grades
if index >= 16:
print("Grade 16+")
elif index < 1:
print("Before Grade 1")
else:
print(f"Grade {index}")
indexOfGrade()
Note: you may optionally remove all the comment statements.