[英]cs50 readability pset6 python this code should count the number of word ,sentence and letter but it didn't
from cs50 import get_string
letter=0
sentence=0
word=0
text = get_string("Text: ")
n = len(text)
for i in range(n):
if text[i].isalnum()==True:
letter=letter+1
for i in range(n):
if text[i].isspace()==True and text[i+1].isalnum()==True:
word=word+1
for i in range(n):
if text[i]=="?" or text[i]=="." or text[i]=="!":
sentence=sentence+1
grade = round (0.0588 * ((100 * letter) / word) - 0.296 * ((100 * sentence) / word) - 15.8)
if grade<1:
print("Before Grade 1")
elif grade < 16:
print(f"Grade {grade}")
else :
print("Grade 16+");
https://submit.cs50.io/check50/d3897b249b2ca6ea937d023c25c1607af52ea5cd the checks is bad https://submit.cs50.io/check50/d3897b249b2ca6ea937d023c25c1607af52ea5cd支票不好
may be i do something false in it可能我做错了什么
You have a couple issues here:你在这里有几个问题:
The first one is using Text at the beginning (not standard, by the way) vs text .第一个是在开头使用Text (顺便说一下,不是标准的)与text 。 That should help you out quite a bit.
这应该对你有很大帮助。
The second is that you aren't actually using the get_string method.第二个是您实际上并没有使用get_string方法。 Now, it's possible this works for you;
现在,这可能适合您; I just threw it into a Juypter Notebook because I wanted to work with it like a REPL.
我只是将它放入 Juypter Notebook 中,因为我想像 REPL 一样使用它。 But, assuming not, let's briefly step through it.
但是,假设没有,让我们简要介绍一下。
Conda environment or pip install with cs50 and run that:使用 cs50 安装 Conda 环境或 pip 并运行:
Runs fine with no errors.运行良好,没有错误。 Good.
好的。 Now the first block:
现在第一个块:
letter=0
sentence=0
word=0
Text = get_string("Text: ")
That (because I'm using a notebook) evaluates as:那(因为我使用的是笔记本)评估为:
Text: None
文字:无
Now, maybe there is something specific you are to use in the cs50 library but I used input -- and changed Text to lower case.现在,也许您要在 cs50 库中使用某些特定的东西,但我使用了输入——并将 Text 更改为小写。
But you were on the right path.但你走在正确的道路上。
There are a couple of problems with the way program counts words.程序计算单词的方式存在一些问题。 It is doing so by counting spaces (good plan), but
它是通过计算空间来实现的(好的计划),但是
if text[i].isspace()==True and text[i+1].isalnum()==True:
only counts spaces followed by az, AZ, or 0-9. if text[i].isspace()==True and text[i+1].isalnum()==True:
只计算后面跟着 az、AZ 或 0-9 的空格。 In this case, it will not count the space after "Alice", so word count is incorrect.thought Alice "without pictures or conversation?"
”爱丽丝想“没有照片或谈话?”
A side note: accessing i + 1
in the i
loop is generally speaking a very bad idea;旁注:在
i
循环中访问i + 1
通常来说是一个非常糟糕的主意; it will be outside the range when i reaches the maximum.当我达到最大值时,它将超出范围。 It hasn't failed in testing because no tested texts end with a space.
它在测试中没有失败,因为没有测试过的文本以空格结尾。 If you want to see it fail, enter a text that ends with a space.
如果您想看到它失败,请输入以空格结尾的文本。
1) Initialized word to 1; 1) 初始化字为1; 2) Added the following to assume that a letter is any lower/uppercase character: for i in range(n): if text[i].isalnum() or text[i].isupper() or text[i].islower() == True: letter = letter+1 3) Removed for word count: and text[i+1].isalnum()==True:
2) 添加以下内容以假设字母是任何小写/大写字符:for i in range(n): if text[i].isalnum() or text[i].isupper() or text[i].islower () == True: letter = letter+1 3) 删除字数:和 text[i+1].isalnum()==True:
Now your code is 10/10!现在您的代码是 10/10!
Try initializing word to 1, removing 'round' before CLI, and adding round to you second to last print statement.尝试将 word 初始化为 1,在 CLI 之前删除 'round',并将round 添加到倒数第二个打印语句。 Example: {round(grade)}
示例:{round(grade)}
I had similar code, worked for me.我有类似的代码,为我工作。
很棒的工作,我修复了它,只在此处添加了最后一部分:
if text[i]=="?" or text[i]=="." or text[i]=="!" or text[i]==":":
您应该分配word = 1
而不是word = 0
(第 4 行)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.