I got an assignment to write a code that creates a dictionary that every key is a letter and every value is the number of that letter in user_str. It needs to look like this:
magic_str = "abra cadabra"
count_chars(magic_str)
{'a': 5, 'b': 2, 'r': 2, 'c': 1, 'd': 1}
And this is my code, I will love to have suggestions on how to upgrade it and why this is the best option, or if there is another option:
def count_chars(my_str):
"""The func take string from user_str and create a dictionary of any letter as key and
and the value is how many from this letter there is in user_str
Args:
my_str (str): the user_str
my_dict (dict): the dictionary of letters and numbers
"""
my_dict = {}
for i in my_str:
if i in my_dict:
my_dict[i] = my_dict[i] + 1
return my_dict
def main():
user_str = input()
count_chars(user_str)
if __name__ == "__main__":
main()