+ 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?
3 Respostas
+ 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)
+ 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
- 1
Thanks very much