+ 1

Letter Counter Project Help

Greetings, I have already looked up answers to this, but I don't want to copy & paste because I don't even get the answers. Please help me modify my wretched code into something feasible, so that I'll understand what's going on. ... Problem: Given a string as input, you need to output how many times each letter appears in the string. You decide to store the data in a dictionary, with the letters as the keys, and the corresponding counts as the values. Create a program to take a string as input and output a dictionary, which represents the letter count. Sample Input hello Sample Output {'h': 1, 'e': 1, 'l': 2, 'o': 1} ... The best code that I have mustered is the following: text = input() dict = {} y = list(text) z = y.count(text) a = {z : y} print(a) """ Input awesome Your Output {0: ['a', 'w', 'e', 's', 'o', 'm', 'e']} ... How would you correct my existing code? Much appreciated, Abraham

25th Feb 2022, 12:48 AM
Abraham Callahan
Abraham Callahan - avatar
3 Respuestas
+ 5
1. Don't use 'dict' as a name, because it will shadow the built-in dict() function. Instead use something like 'd' or better yet a meaningful name, such as 'letters'. 2. No need to convert the string to a list. A string in python is already a sequence type, so it is iterable as is. 3. You can iterate (loop) over the strings characters and check if the current character is in (or not in) the letters dictionary. If it is, then increment its count by one. If not, then add it to the dictionary and give it a value of 1. There are other ways to accomplish this task, but this is the intent of the lesson. Not using a Counter object or the count function etc. Refer back to the lessons on for loops and dictionaries (adding to a dict and accessing the value for a given key).
25th Feb 2022, 1:06 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
ChaoticDawg Thank you very kindly. 1. dict = {} was Sololearn's setup, but I will keep your lesson in mind for the future. 2. I couldn't figure out how to break up the input words into letters. By 'strings are iterable', I understand that as 'the individual character entries of a string can be processed in a looping fashion as a sequenced list of items'. 3. In other solutions, I did see coding like "count =0" ... "count += 1". I assume that is what you're also describing. Many thanks again! I'm still struggling with this problem for the moment, but you have helped me better understand the problem.
25th Feb 2022, 1:55 AM
Abraham Callahan
Abraham Callahan - avatar
+ 3
You can loop through the characters in a string like; # where text is the var name for a string # I.E. returned value from input() # 'abcde' for ch in text: ... # ch will be the current character This would be similar to; for el in ['a', 'b', 'c', 'd', 'e']: ... While 'abcde' is not the same as ['a', 'b', 'c', 'd', 'e'] they can be looped through in the same way as they are both sequences or iterable types. They can also be indexed in a similar fashion to get the value at a particular index. However, since a string is not a mutable type the value at that index cannot be changed like it could with a list. Hope that helps you further.
25th Feb 2022, 2:08 AM
ChaoticDawg
ChaoticDawg - avatar