Reputation: 11
very new to Python. I have a definition that I can get to print everything I want with print(), but am not able to find a way to return all that information.
For example, when counting the number of letters, the number and percentage of Es (lower and upper), I want to return the following to be printed by a test:
"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
An example of test:
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)
Upvotes: 0
Views: 2431
Reputation: 119
This should suffice:
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'.")
Upvotes: 0
Reputation: 33359
return "The text contains %d alphabetic characters, of which %d (%f%%) are 'e'." % (alpha_count, ecount, letter_percent(ecount, alpha_count)
Upvotes: 1