计算字母字符数的Python函数,跟踪字母字符的数量“;e&x201D;出现,使用。isalpha
我必须编写一个函数来接收字符串作为输入。 我的函数应该计算文本中字母字符(a到z,或a到z)的数量,并记录字母“e”(大写或小写)出现的次数 我的函数应该返回对文本的分析,如下所示:计算字母字符数的Python函数,跟踪字母字符的数量“;e&x201D;出现,使用。isalpha,python,string,python-3.x,Python,String,Python 3.x,我必须编写一个函数来接收字符串作为输入。 我的函数应该计算文本中字母字符(a到z,或a到z)的数量,并记录字母“e”(大写或小写)出现的次数 我的函数应该返回对文本的分析,如下所示: "a".isalpha() # => evaluates to True "3".isalpha() # => evaluates to False "&".isalpha() # => False " ".isalpha() # => False mystr = "Q" myst
"a".isalpha() # => evaluates to True
"3".isalpha() # => evaluates to False
"&".isalpha() # => False
" ".isalpha() # => False
mystr = "Q"
mystr.isalpha() # => True
def analyze_text(text):
text = "".join([i for i in text if i.isalpha()])
char = len(text)
char_e = text.lower().count('e')
if char_e:
char_ediv = str(char/char_e * 100.0)
else:
char_ediv = str(0.0)
result = ("The text contains {} alphabetic characters, of which {} ({} %) are 'e'."
"".format(char, char_e, char_ediv))
return result
文本包含240个字母字符,其中105个(43.75%)为“e”
我需要使用str.isalpha
函数,它的用法如下:
"a".isalpha() # => evaluates to True
"3".isalpha() # => evaluates to False
"&".isalpha() # => False
" ".isalpha() # => False
mystr = "Q"
mystr.isalpha() # => True
def analyze_text(text):
text = "".join([i for i in text if i.isalpha()])
char = len(text)
char_e = text.lower().count('e')
if char_e:
char_ediv = str(char/char_e * 100.0)
else:
char_ediv = str(0.0)
result = ("The text contains {} alphabetic characters, of which {} ({} %) are 'e'."
"".format(char, char_e, char_ediv))
return result
我正在努力使用.isalpha函数,我假设我们正在尝试使用它来区分字母字符和特殊字符,例如:“,?!/”
…等等。我只是不完全确定如何使用它。我看过Stackoverflow,并试图用一些人使用它的方式来使用它。这是我目前的代码:
def analyze_text(text):
"".join(i for i in text if i.isalpha())
char = text.count('') - 1
char_e = text.count('e') + text.count('E')
char_ediv = str(char/char_e * 100.0)
result = print("The text contains", char, "alphabetic characters, of which", char_e,"("+ char_ediv + "%" + ")" " are 'e'.")
return result
我的字符串应通过以下测试:
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)
到目前为止,我已经通过了第一次测试,然后我得到一个错误:
Error
UnboundLocalError: **local variable 'result' referenced before assignment on line 10**
Description
undefined
To Fix
undefined
另一个错误发生在第三个测试用例中,因为您没有“e”,因此被零除 修复方法是使用
,如果字符:
def analyze_text(text):
text = "".join(i for i in text if i.isalpha())
char = text.count('') - 1
char_e = text.count('e') + text.count('E')
if char_e:
char_ediv = str(char/char_e * 100.0)
else:
char_ediv = str(0.0)
然而,有些评论:
- 您有
“”.join(如果i.isalpha(),则在文本中为i指定i)
,而不为其分配变量。我在上面解决了这个问题
- 您可以使用
text.lower().count('e')
而不是countinge
和e
- 您不需要计数
”
,只需使用len()
计数所有剩余字符即可
print
返回None
,因此您总是返回None
无论何时多次使用字符串加法,都可以将其替换为以下格式:
"The text contains", char, "alphabetic characters, of which", char_e,"("+ char_ediv + "%" + ")" " are 'e'."
最好是:
result = ("The text contains {} alphabetic characters, of which {} ({} %) are 'e'."
"".format(char, char_e, char_ediv))
更新后的函数如下所示:
"a".isalpha() # => evaluates to True
"3".isalpha() # => evaluates to False
"&".isalpha() # => False
" ".isalpha() # => False
mystr = "Q"
mystr.isalpha() # => True
def analyze_text(text):
text = "".join([i for i in text if i.isalpha()])
char = len(text)
char_e = text.lower().count('e')
if char_e:
char_ediv = str(char/char_e * 100.0)
else:
char_ediv = str(0.0)
result = ("The text contains {} alphabetic characters, of which {} ({} %) are 'e'."
"".format(char, char_e, char_ediv))
return result
print()
函数在控制台中写入一个字符串,即“prints”,这与函数返回的不同。您需要返回正在打印的字符串
result = "The text contains" + char + "alphabetic characters, of which" + char_e + "(" + char_ediv + "%" + ")" " are 'e'."
return result
您描述的异常对于所显示的代码没有任何意义。你能给我们完整的回溯(格式正确)吗?您的代码存在许多问题(例如向后除法,以及试图保存print
的返回值),但这些问题会导致您描述的以外的其他问题。