-1

I'm trying to create a program that takes in text from a file and outputs the frequency of each letter. The output also shows the frequency of '.' but I only want to output the frequency of letters. I'm trying to use isalpha() but I'm not sure where to put it. This is my code so far:

def count(str):
    d = dict()
    word = str.lower()

    for i in word:
        for j in i:
            if j in d:
                d[j] = d[j] + 1
            else:
                d[j] = 1
    return d

print(count("Once upon a time there lived a princess who was too beautiful and kind to her subjects. The poets and artists were all in praise of her beauty and their works were inspired by her. Once a charming prince from another kingdom came to meet the princess and soon they became friends and slowly they fell in love")) 

I tried putting isalpha() here:

for i in word.isalpha():

but it just gives me an error.

amelia
  • 9
  • 2
  • 3
    "but it just give me an error." -- what does the error say, please and which line of code does it point to? Usually errors tell you exactly what went wrong. You're almost there, just add another `if` before trying the dictionary lookup/set and check each character. Also, don't call a variable `str` -- that overwrites a builtin. The name `word` is misleading because that's presumably your whole file contents, not a single word. – ggorlen Jan 25 '22 at 01:32
  • Be more specific. Where and how did you try `.isalpha()` method, and how did it fail? – KokoseiJ Jan 25 '22 at 01:34
  • 2
    `isalpha` returns a boolean value. You want to write `if word.isalpha()` (although, it's not a word, it's a character) – Kraigolas Jan 25 '22 at 01:35
  • 1
    `for i in word.isalpha()` is like saying `for i in True` or `for i in False` – Pepsi-Joe Jan 25 '22 at 01:37
  • 1
    Does this answer your question? [Counting only the frequency of letters in a string](https://stackoverflow.com/questions/49557776/counting-only-the-frequency-of-letters-in-a-string) – Tomerikoo Jan 25 '22 at 22:03
  • Why did this get closed as needing details? It's definitely a dupe, as Tomerikoo pointed out. – CrazyChucky Jan 25 '22 at 22:07

2 Answers2

1
from string import ascii_lowercase as alphabet 
# or simply declare: alphabet = 'abcdefghijklmnopqrstuvwxyz' 
def letter_frequency(text):    
    frequency = {letter:0 for letter in alphabet}
    for character in text.lower():
        try:
            frequency[character] += 1
        except KeyError:
            pass
    return frequency
Michael Ekoka
  • 19,050
  • 12
  • 78
  • 79
-1
import string

alphabet_string = string.ascii_lowercase
alphabet_list = list(alphabet_string)

letter_count = {}


string = (
    "Once upon a time there lived a princess who was too beautiful and" 
    "kind to her subjects.The poets and artists were all in praise of" 
    "her beauty and their works were inspired"
)

for char in alphabet_list:
    letter_count[char] = 0
for char in string:
    if char in alphabet_list:
        letter_count[char] += 1
print (letter_count)