0

How do I make it count a letter that appears more than once?

text = input() dict = {} for i in text: dict[i] = dict.get(text, len(i)) print(dict)

11th Nov 2022, 7:25 PM
Ali Najeeb
Ali Najeeb - avatar
1 Answer
+ 1
try this: letters = {} for i in text: letters[i] = letters.get(i, 0) + 1 I would advice not to use dict as a variable name since it's a reserved keyword in python. The get method with 2 parameters allows you to set a default value if the key does not exists. For example, if "a" is not a key in the letters dictionary, letters.get("a", 0) will return 0. I hope this helps :)
11th Nov 2022, 7:39 PM
Apollo-Roboto
Apollo-Roboto - avatar