0

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)
cs95
  • 379,657
  • 97
  • 704
  • 746
Wayne STL
  • 11
  • 1
  • In future, please pay more attention to your formatting. Presentation is important when asking a question you want to be well received. – cs95 Aug 12 '17 at 03:22
  • This is my first ever time on this website. I'm only 3 weeks into a programming course. I will strive to learn the proper etiquette in such a niche community. Please be patient. If you're going to flag my question as redundant/already answered, or say my formatting is wrong, please ALSO advise me on where to look and also on how to better format in the future, as I HAVE NO CLUE BECAUS I'M BRAND NEW TO THIS. See "very new to Python." above, thanks. – Wayne STL Aug 12 '17 at 03:30
  • I have googled the heck out of it, and cannot figure out how to return my string formatted properly. – Wayne STL Aug 12 '17 at 03:31
  • When pasting code, highlight everything and hit Ctrl+K. For help on how to solve your problem, look at the marked duplicates. If those don't help, let me know how your question is different from theirs, and I'll reopen it. – cs95 Aug 12 '17 at 03:32
  • I found that this returns partially correct, but includes unnecessary commas: return "The text contains", alpha_count, "alphabetic characters, of which", ecount, "(",letter_percent(ecount, alpha_count),")% are 'e'." – Wayne STL Aug 12 '17 at 03:33
  • Your function does not have any return statement... did you post all your code here? – cs95 Aug 12 '17 at 03:34
  • I did not post all of the code, no. In the comments, I posted the bit I was returning (see above). I don't know how stack overflow works. I am in a beginner course and only have weekend nights to work on it, when all my teaching fellows are out partying. SO, I figured I'd try here. But, if the great and mighty nerd-wizard coldspeed is any indication of this community, I have a feeling I won't get much actual help in the future since I'm a "noob." – Wayne STL Aug 12 '17 at 03:40
  • I'm now at a point where I cannot figure out how to omit unwanted commas from my return statement. I have it otherwise figured out. – Wayne STL Aug 12 '17 at 03:41
  • Reopened this, on closer inspection is not a duplicate. :) – cs95 Aug 12 '17 at 04:05

2 Answers2

1
return "The text contains %d alphabetic characters, of which %d (%f%%) are 'e'." % (alpha_count, ecount, letter_percent(ecount, alpha_count)
John Gordon
  • 29,573
  • 7
  • 33
  • 58
0

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'.")
Jason Atallah
  • 109
  • 1
  • 6