非常新的Python。我有一个定义,我可以使用print()打印我想要的所有内容,但我无法找到返回所有信息的方法。
例如,当计算字母数,Es的数量和百分比(下限和上限)时,我想通过测试返回以下内容:
"The text contains ", alpha_count, " alphabetic characters, of which ", ecount," (", letter_percent(ecount, alpha_count),"%) are 'e'."
import math
def analyze_text(text):
ecount = text.count("E") + text.count("e")
alpha_count = 0
def letter_percent(ecount, alpha_count):
return 100 * float(ecount)/float(alpha_count)
for letter in text:
if letter.isalpha() ==True:
alpha_count += 1
测试的一个例子:
from test import testEqual
text2 = "Blueberries are tasteee!"
answer2 = "The text contains 21 alphabetic characters, of which 7 (33.3333333333%) are 'e'."
testEqual(analyze_text(text2), answer2)
答案 0 :(得分:0)
这应该足够了:
def ecounter(text):
text = text.lower()
ecount = 0
alpha_count = 0
for i in text:
if i == "e":
ecount += 1
if i.isalpha() == True:
alpha_count += 1
percent = (ecount/alpha_count) * 100
print("The text contains", alpha_count, "alphabetic characters, of which", ecount, "(", percent, "%) are 'e'.")