+ 1

letter counter

text = input() text_dict = {} #your code goes here text_list = list(text) text_list2 = list(text) for i in range(len(text)): for x in range(len(text)): y = 0 if text_list[x] == text_list2[x]: y = y + 1 text_dict[text_list[i]] = y print(text_dict) can someone please tell me what’s wrong with this? it’s meant to print the number of letters in a word in dictionary format

23rd Mar 2022, 11:18 AM
Oliver Clayden
4 Answers
+ 2
What does your if condition do? It's same as if True:
23rd Mar 2022, 11:38 AM
Simba
Simba - avatar
+ 1
Oliver clayden , just some thoughts from my side: â–Șwe do not need 2 text lists â–Șwe need only one for loop it could be done this way: text_list = list(input()) text_dict = {} #text_list = list(text) # is done with input directly #text_list2 = list(text) # not required for char in text_list: if char in text_dict: # if character already exists: increment number text_dict[char] += 1 else: # create new element in dict and set value to 1 text_dict[char] = 1 print(text_dict)
23rd Mar 2022, 5:49 PM
Lothar
Lothar - avatar
0
text = input() text_dict = {} #your code goes here text_list = list(text) text_list2 = list(text) for i in range(len(text)): y = 0 for x in range(len(text)): if text_list[i] == text[x]: y = y + 1 text_dict[text_list[i]] = y print(text_dict)
23rd Mar 2022, 11:41 AM
Oliver Clayden
0
just solved it
23rd Mar 2022, 11:41 AM
Oliver Clayden