+ 1

How can I output only the last result of the code below?

text = input() dict = {} #your code goes here for x in text: dict[x] = text.count(x) print(dict) ☝️☝️ The above code will output good {'g': 1} {'g': 1, 'o': 2} {'g': 1, 'o': 2} {'g': 1, 'o': 2, 'd': 1} ☝️☝️This But I only wanted it to output only {'g': 1, 'o': 2, 'd': 1} this, how can I do that?

28th Jul 2022, 4:49 AM
No One
3 Respuestas
+ 1
the print statement seems to be indented and that is why you are getting dict printed on each iteration of the for loop. In short: delete the spaces behind print(dict)
28th Jul 2022, 4:54 AM
YuGiMob
YuGiMob - avatar
+ 5
No One , a good and correct indentation gives a good readability of the code. it is recommended to use 4 spaces per indentation level. this is the official style guide for python (PEP 8): https://peps.python.org/pep-0008/#indentation
28th Jul 2022, 5:43 AM
Lothar
Lothar - avatar
- 1
Thanks very much
28th Jul 2022, 5:06 AM
No One