我正在研究这个问题而找不到合适的答案,但我已经设法挖了一个更大的漏洞。迷惑自己。所以如果有人能提供清晰度:
方向:
编写一个函数analyze_text,它接收一个字符串作为输入。您的函数应该计算文本中字母字符的数量(a到z,或A到Z),并且还要跟踪字母的数量' e' (大写或小写)。
你的函数应该以字符串形式返回对文本的分析,其语法如下:
“该文字包含240个字母字符,其中105(43.75%)为'e'。”
您需要使用isalpha功能。
我的代码到目前为止: def analyze_text(text): count = 0 letter_count = 0
for char in text:
if char.isalpha():
count += 1
for e in text:
if e == "e" or e =="E":
letter_count += 1
p = float(letter_count)/float(count) * 100
analyze.text = "The text contains {0} alphabetic characters, of
which {1} ({2}) are 'e'."
print(analyze_text.format(count += 1, letter_count += 1, p))
TESTS that are given:
# Note that depending on whether you use str.format or
string concatenation
# your code will pass different tests. Code passes either
# tests 1-3 OR tests 4-6.
from test import testEqual
# Tests 1-3: solutions using string concatenation should pass these
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)
# Tests 4-6: solutions using str.format should pass these
text4 = "Eeeee"
answer4 = "The text contains 5 alphabetic characters,
of which 5 (100%) are 'e'."
testEqual(analyze_text(text4), answer4)
text5 = "Blueberries are tasteee!"
answer5 = "The text contains 21 alphabetic characters,
of which 7 (33.33333333333333%) are 'e'."
testEqual(analyze_text(text5), answer5)
text6 = "Wright's book, Gadsby, contains a total of
0 of that most common symbol ;)"
answer6 = "The text contains 55 alphabetic characters,
of which 0 (0%) are 'e'."
testEqual(analyze_text(text6), answer6)
答案 0 :(得分:0)
评论中只列出了一些错误:
def analyze_text(text):
count = 0
letter_count = 0
for char in text:
if char.isalpha():
count += 1
for e in text:
if e == "e" or e == "E":
letter_count += 1
p = float(letter_count) / float(count) * 100
# here, you put a period instead of an underscore in analyze_text
# you also forgot to put the percent sign "%"
analyze_text = "The text contains {0} alphabetic characters, of which {1} ({2}%) are 'e'."
# you don't need to add 1 to count and letter_count using += 1
# they are already the correct values
# also, you should return the string here, not print it
return analyze_text.format(count, letter_count, p)
该代码可以为您提供您在问题中显示的所需结果
答案 1 :(得分:0)
def analyze_text(text, letter='e'):
n = len([x for x in text if x.isalpha()])
freq = text.lower().count(letter)
percent = float(freq) / n * 100
return "The text contains {} alphabetic characters, of which {} ({}%) are '{}'.".format(n, freq, percent, letter)
答案 2 :(得分:0)
快速浏览一下您的工作,看起来好像您错误地使用了替换字段。你的代码:
print(analyze_text.format(count += 1, letter_count += 1, p))
正确版本:
print(output.format(count, letter_count, p))
以下是您的功能应该是什么样的:
def analyze_text(text):
count = 0
letter_count = 0
for char in text:
if char.isalpha():
count += 1
for e in text:
if e == "e" or e == "E":
letter_count += 1
p = float(letter_count)/float(count) * 100
output = "The text contains {0} alphabetic characters, of which {1} ({2}%) are 'e'."
print(output.format(count, letter_count, p))
您应该将格式化程序视为列表。您的替换字段对应于列表中的顺序。希望澄清一切。
P.S。 - 您忘记了输出消息中的%符号。