python - Counting, finding certain letter and percentage -


i working on question , cannot find right answer have managed dig bigger hole & confuse myself. if can provide clarity:

directions:

write function analyze_text receives string input. function should count number of alphabetic characters (a through z, or through z) in text , keep track of how many letter 'e' (upper or lowercase).

your function should return analysis of text in form of string phrased this:

“the text contains 240 alphabetic characters, of 105 (43.75%) ‘e’.”

you need make use of isalpha function.

my code far: def analyze_text(text): count = 0 letter_count = 0

for char in text:     if char.isalpha():         count += 1 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  {1} ({2}) 'e'." print(analyze_text.format(count += 1, letter_count += 1, p))  tests given: # note depending on whether use str.format or  string concatenation  # code pass different tests. code passes either # tests 1-3 or tests 4-6.  test import testequal  # tests 1-3: solutions using string concatenation should pass these text1 = "eeeee" answer1 = "the text contains 5 alphabetic characters,  of 5 (100.0%) 'e'." testequal(analyze_text(text1), answer1)  text2 = "blueberries tasteee!" answer2 = "the text contains 21 alphabetic characters, of  7 (33.3333333333%) 'e'." testequal(analyze_text(text2), answer2)  text3 = "wright's book, gadsby, contains total of 0 of  common symbol ;)" answer3 = "the text contains 55 alphabetic characters,  of 0 (0.0%) '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 5 (100%) 'e'." testequal(analyze_text(text4), answer4)  text5 = "blueberries tasteee!" answer5 = "the text contains 21 alphabetic characters,  of 7 (33.33333333333333%) 'e'." testequal(analyze_text(text5), answer5)  text6 = "wright's book, gadsby, contains total of  0 of common symbol ;)" answer6 = "the text contains 55 alphabetic characters,  of 0 (0%)     'e'." testequal(analyze_text(text6), answer6) 

there couple things wrong outlined below in comments:

def analyze_text(text):     count = 0     letter_count = 0     char in text:         if char.isalpha():             count += 1     e in text:         if e == "e" or e == "e":             letter_count += 1     p = float(letter_count) / float(count) * 100      # here, put period instead of underscore in analyze_text     # forgot put percent sign "%"     analyze_text = "the text contains {0} alphabetic characters, of {1} ({2}%) 'e'."      # don't need add 1 count , letter_count using += 1     # correct values     # also, should return string here, not print     return analyze_text.format(count, letter_count, p) 

that code should desired results showed in question


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -