簡體   English   中英

計算字母字符數的 Python 函數,跟蹤“e”出現的次數,利用 .isalpha

[英]Python Function that counts number of alphabetic characters, keeps track of how many times “e” Appears, utilizes .isalpha

我必須編寫一個接收字符串作為輸入的函數。 我的函數應該計算文本中字母字符(a 到 z,或 A 到 Z)的數量,並跟蹤字母“e”(大寫或小寫)出現的次數。

我的函數應該返回對文本的分析,如下所示:

文本包含 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

我正在努力使用 .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

print()函數在控制台中寫入,即“打印”一個字符串,這與函數的return不同。 您需要返回正在打印的字符串。

result = "The text contains" + char + "alphabetic characters, of which" + char_e + "(" + char_ediv + "%" + ")" " are 'e'."
return result

另一個錯誤發生在第三個測試用例中,因為您沒有“e”,因此您除以零。

解決方法是使用if char_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 for i in text if i.isalpha())沒有給它分配一個變量。 我在上面解決了這個問題。

  • 您可以使用text.lower().count('e')而不是計算eE

  • 你不需要計算'' ,你可以簡單地使用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))

更新后的函數如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM