2016-11-20 112 views
0

編寫一個函數analyze_text,它接收一個字符串作爲輸入。函數應計算文本中的字母字符數(a到z或從A到Z),並記錄字母'e'(大寫或小寫)的數量。計算字母數字的Python函數,記錄多少次「e」出現

該函數應返回文本的分析,是這樣的:

文本包含240個字母字符,其中105(43.75%)的是「E」。

我需要使用isalpha功能,它可以這樣使用的:

"a".isalpha() # => evaluates to True 
"3".isalpha() # => evaluates to False 
"&".isalpha() # => False 
" ".isalpha() # => False 

mystr = "Q" 
mystr.isalpha() # => True 

功能應通過以下測試:

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) 

所以,我想:

def analyze_text(text): 

    text = input("Enter some text") 
    alphaChars = len(text) 

#count the number of times "e" appears 
    eChars = text.count('e') 

#find percentage that "e" appears 
    eCharsPercent = eChars/alphaChars 

    print("The text contains" + alphaChars + "alphabetic characters, of 
    which" + eChars + "(" + eCharsPercent + ") are 'e'.") 

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) 

正如你所看到的,我嘗試過的並不使用isalpha函數(我不知道如何/在哪裏使用我T)。此外,無論測試是否通過,函數都不會返回。可視化python不支持「測試」,我在書中使用的文本編輯器說我有縮進錯誤(?)我不知道從哪裏開始 - 請幫助。

Screenshot of Book Text Editor

EDIT:現在接收 「類型錯誤:不能連接於線12 'STR' 和 'INT' 對象」(即與 「打印」 開頭的行)。

+1

in analyse_text print語句在2行中沒有反斜槓。這是您的其中一個錯誤... –

+1

縮進錯誤意味着您沒有正確縮進您的代碼。由於Python使用縮進而不是大括號({,})對塊進行分組,因此需要確保正確使用空格和製表符。不要混合標籤和空格,並且始終使用相同數量的標籤/空格。 – anroesti

+0

@ Jean-FrançoisFabre你是對的我錯過了一個「)」。我解決了這個問題,現在我收到一條新的錯誤消息:TypeError:不能連接第12行的'str'和'int'對象。這是以「print」開頭的行。 – Sean

回答

1

這裏是一個analyse_text功能通過測試:

def analyze_text(text): 
    filtered = [c.lower() for c in text if c.isalpha()] 
    cnt = filtered.count('e') 
    result = "The text contains {} alphabetic characters, of which {} ({}%) are 'e'.".format(len(filtered),cnt,str(100.0*cnt/len(filtered))[:13]) 
    return result 
  • 創建所有alphanum字符,小寫的列表,用一個漂亮的列表理解(也就是你失蹤的部分),創建filtered變量
  • count count e(you got that right),創建cnt計數器
  • 格式字符串accordignly(使用了一點破解得到33.3333333 ri ght,或許可以做更好的事情)。創建result字符串後返回
+0

因爲我剛剛學習python(和編碼),analyze_text是一個函數名稱 - 但什麼是「過濾」,「cnt」和「結果」?變量?除此之外,你的答案是有道理的,謝謝。 – Sean

+0

我在提交作業時遇到了問題。結果需要返回5個數字以完全匹配解決方案。看這個截圖:http://i.imgur.com/6zi5e2c。jpg – Sean

+0

不要緊,我能弄明白 - 將拼接從[:13]改爲[:18],再強制5個數字。謝謝。 – Sean

相關問題