计算字母字符数的 Python 函数,跟踪 "e"出现的次数

标签 python string function testing character

编写一个接收字符串作为输入的函数analyze_text。该函数应计算文本中字母字符(a 到 z,或 A 到 Z)的数量,并跟踪字母“e”(大写或小写)的数量。

该函数应该返回对文本的分析,如下所示:

文本包含 240 个字母字符,其中 105 个(43.75%)是“e”。

我需要使用 isalpha 函数,可以这样使用:

"a".isalpha() # => evaluates to True
"3".isalpha() # => evaluates to False
"&".isalpha() # => False
" ".isalpha() # => False

mystr = "Q"
mystr.isalpha() # => True

该函数应通过以下测试:

from test import testEqual

text1 = "Eeeee"
answer1 = "The text contains 5 alphabetic characters, of which 5 
(100.0%) are 'e'."

testEqual(analyze_text(text1), answer1)

text2 = "Blueberries are tasteee!"
answer2 = "The text contains 21 alphabetic characters, of which 7
(33.3333333333%) are 'e'."

testEqual(analyze_text(text2), answer2)

text3 = "Wright's book, Gadsby, contains a total of 0 of that most
common symbol ;)"

answer3 = "The text contains 55 alphabetic characters, of which 0
(0.0%) are 'e'."

testEqual(analyze_text(text3), answer3)

所以我尝试了:

def analyze_text(text):

    text = input("Enter some text")
    alphaChars = len(text)

#count the number of times "e" appears
    eChars = text.count('e')

#find percentage that "e" appears
    eCharsPercent = eChars / alphaChars

    print("The text contains" + alphaChars + "alphabetic characters, of 
    which" + eChars + "(" + eCharsPercent + ") are 'e'.")

from test import testEqual

text1 = "Eeeee"
answer1 = "The text contains 5 alphabetic characters, of which 5
(100.0%) are 'e'."

testEqual(analyze_text(text1), answer1)

text2 = "Blueberries are tasteee!"
answer2 = "The text contains 21 alphabetic characters, of which 7
(33.3333333333%) are 'e'."

testEqual(analyze_text(text2), answer2)

text3 = "Wright's book, Gadsby, contains a total of 0 of that most
common symbol ;)"
answer3 = "The text contains 55 alphabetic characters, of which 0
(0.0%) are 'e'."

testEqual(analyze_text(text3), answer3)

如您所见,我的尝试没有使用 isalpha 函数(我不知道如何/在何处使用它)。此外,该函数不会返回测试是否通过。 Visualize python 不支持“测试”,我在书中使用的文本编辑器说我有缩进错误(?)我不知道从哪里开始 - 请帮忙。

Screenshot of Book Text Editor

编辑:现在收到“TypeError:无法在第 12 行连接‘str’和‘int’对象”(以“print”开头的行)。

最佳答案

这是一个通过测试的 analyse_text 函数:

def analyze_text(text):
    filtered = [c.lower() for c in text if c.isalpha()]
    cnt = filtered.count('e')
    result = "The text contains {} alphabetic characters, of which {} ({}%) are 'e'.".format(len(filtered),cnt,str(100.0*cnt/len(filtered))[:13])
    return result
  • 创建所有字母数字字符的列表,小写,使用一个很好的列表理解(这是你缺少的部分),创建过滤变量
  • 计算字母 e(你没看错),创建 cnt 计数器
  • 自动格式化字符串(使用了一些 hack 来使 33.3333333 正确,也许可以做一些更好的事情)。获取确切的字符串有点毫无意义...,创建 result 字符串,该字符串在
  • 之后返回

关于计算字母字符数的 Python 函数,跟踪 "e"出现的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40710190/

相关文章:

R:找到作为参数传递的函数的原始名称

java - Android - 调用对象时崩溃

python - pyodbc 在 sp_prepexec 之后调用 sp_unprepare。这会影响参数化查询的性能吗?

python - 解释Python装饰器是如何工作的

python - 这段使用 lambda 的 python 代码有什么问题?

python - 如何为数据框中的列中的字符串创建规则?

c - 在 C 中序列化函数

python - 什么是 termios.TIOCGWINSZ

c# - 如何在 C# 中删除或替换 "\"

javascript - 连接字符串作为对象属性