cs50 可读性 pset6 python - 索引计算问题
cs50 readability pset6 python - Index calculation Issue
我的 pset6 可读性中的索引计算问题
这是我的代码,我认为它是正确的,但是,每段的评分都是错误的,因为索引计算是错误的。虽然,字母、单词和句子计数是正确的。那么,问题出在哪里呢?有人帮帮我吗?
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,经过很多时间我弄清楚了你的错误。
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
最后一行return只是return的值,不会改变变量words的值,所以需要更正如下
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
此外,字母、单词和句子变量可以选择为 float 而不是 int,并且在计算索引时,python 将省略剩余的小数部分,因此舍入可能无效。
另外,我在我的设备上执行了 check50,所有结果都是绿色的(正确)
此外,if text[i].isspace() and text[i + 1].isspace():
是错误的,您需要完全删除该部分。
因此这里是经过必要更改的最终答案。
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()
注意:您可以选择删除所有评论语句。
我的 pset6 可读性中的索引计算问题
这是我的代码,我认为它是正确的,但是,每段的评分都是错误的,因为索引计算是错误的。虽然,字母、单词和句子计数是正确的。那么,问题出在哪里呢?有人帮帮我吗?
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,经过很多时间我弄清楚了你的错误。
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
最后一行return只是return的值,不会改变变量words的值,所以需要更正如下
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
此外,字母、单词和句子变量可以选择为 float 而不是 int,并且在计算索引时,python 将省略剩余的小数部分,因此舍入可能无效。
另外,我在我的设备上执行了 check50,所有结果都是绿色的(正确)
此外,if text[i].isspace() and text[i + 1].isspace():
是错误的,您需要完全删除该部分。
因此这里是经过必要更改的最终答案。
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()
注意:您可以选择删除所有评论语句。