Error in my code. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Error in my code.

It's my task: https://www.sololearn.com/coach/1060?ref=app It's my code: text = input() text = list(text) my_dict = {} x = 1 for i in range(len(text)): if text[i] not in my_dict: my_dict[text[i]] = x elif text[i] in my_dict: my_dict[text[i]] = x+1 print(my_dict) What's my error?

5th Apr 2021, 8:37 PM
ᙢᖇ.ᚪ ᚱ ع ℰ 爪 Ꭿ ກ
ᙢᖇ.ᚪ ᚱ ع ℰ 爪 Ꭿ ກ - avatar
2 Answers
+ 1
text = input() text = list(text) my_dict = {} x = 1 for i in range(len(text)): if text[i] not in my_dict: my_dict[text[i]] = x elif text[i] in my_dict: my_dict[text[i]] = x+1 print(my_dict)
5th Apr 2021, 8:38 PM
ᙢᖇ.ᚪ ᚱ ع ℰ 爪 Ꭿ ກ
ᙢᖇ.ᚪ ᚱ ع ℰ 爪 Ꭿ ກ - avatar
+ 2
i’m guessing your error is that you are letter count maxes out at 2. in your elif statement you have dict = x+1. in this case x is always equal to 1, so x+ 1 will equal 2. in any other case, you would need a x += 1 or x = x+1 for the value of x to increase. for your answer though, i think all you need to do is replace x+1 with my_dict[text[i]] + 1 so it will add 1 to the existing count value.
5th Apr 2021, 10:37 PM
you are smart. you are brave.
you are smart. you are brave. - avatar