Count letters

--Originally published at Fundamentos de Programación

This was a challenge by Ken from long ago (sorry for posting too late) but it’s bettter later than never.

1d4bjdm_700wa_0

This was a simple program: we asked the user for an imput and the program counted the times that each letter appeared in the text.

The solution is quite simple as you can see

count letters.PNG

3m2apw2_700wa_0

First we create a list with all the letters (or only with the eltters that you want to count). Then we create a function that creates a dictionary to store the key values of each letter (P.D. the number of times that the letter appears on the text). the function then makes the users input lowercase so we avoid counting for example “A” and “a” as different letters.

mrpzwwl_700wa_0

For each letter in the list (it can be other things, not only letters) the program counts the times the letter appears with the built in function count (text.count(the character that you want to count)) and proceeds to add the key and value to the dictionary only if the letter actually appears in the text written (to avoid printing all the letters).

We finally just print the dictionary that the function created and… TA-DA!

lmzj0bm_700wa_0

And in case you were felling down check out this cute video :3

You can check out the full code on github: https://github.com/Guemez/TC1014/blob/master/countletters.py

Follow me on twitter @danigguemez

#TC101 #Python #DailyChallenge #dictionaries