【发布时间】:2020-12-01 05:01:36
【问题描述】:
```
from cs50 import get_string
text = get_string("Text: ")
letters = sentences = 0
words = 1
for i in text:
if i.isalpha():
letters += 1
if i.isspace():
words += 1
if i in ['!', '.', '?']:
sentences += 1
L = (letters / words) * 100
S = (sentences / words) * 100
index = round(0.0588 * L - 0.0296 * S - 15.8)
if index < 1:
print("Before Grade 1")
elif index >= 16:
print("Grade 16+")
else:
print(f"Grade {index}")
```
我正在做一个 cs50 的任务来提高 python 的可读性,但答案总是与 cs50 网站的手册指南中写的不一样。例如,用于输入;
文字:恭喜!今天是你的好日子。你要去伟大的地方! 你已经离开了!
它应该打印“Grade 3”,但我的代码打印的是“Grade 11”,我认为我的代码很好,而且我使用的公式符合网站上的指南。我在stackoverflow上尝试了与此相关的各种解决方案,但仍然没有得到正确的答案,我做错了什么?
【问题讨论】:
-
您的代码为
L和S计算的值是多少?这符合你的预期吗?另外,你有没有试过和教授谈谈这个? -
为什么不把问题发到cs50.stackexchange.com?
-
对照规范检查索引方程。有一个错误。
-
哦,是的,找到了!非常感谢你提醒我