计数,发现某些字母和百分比
问题描述:
我正在处理这个问题,但找不到正确的答案,但我设法挖了一个更大的洞&迷惑自己。因此,如果任何人都可以提供清晰:计数,发现某些字母和百分比
方向:
写接收一个字符串作为输入的功能analyze_text。您的函数应计算文本中的字母字符数(a到z或从A到Z),并记录字母'e'(大写或小写)的数量。
你的功能应该在一个字符串的确切措辞这样的形式返回文本的分析:“文本包含240个字母字符,其中105(43.75%)的是‘E’”
您将需要使用isalpha函数。
到目前为止我的代码: 高清analyze_text(文本): 计数= 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)
答
有错误的评论概述如下只有两件事情:
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)
该代码应该为您提供您在问题中显示的所需结果
答
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)
答
快速浏览一下您的工作,看起来好像您错误地使用了替换字段。您的代码:
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. - 您在输出讯息中忘记了%符号。
你需要显示你所得到的错误,否则你将不会得到任何帮助,但你会得到大量的选票 – aydow
非常接近,但不是打印答案字符串,你需要返回它。此外,通过在同一个循环内增加count和letter_count,您可能会更有效率。 –