Nov-08-2019, 11:38 AM
I have a dictionary that gives me a letter of the alphabet as a key and a number that corresponds to the position of the letter of the alphabet.
I created the dictionary to do it, but I don't know how to do the sum inside the dictionary because it's not a string.
To better understand what I want:
Never mind I resolved!!!
Quote:{'a':1 'b':2 ... 'z':26}I would like that if I take a string, make the sum of the individual values.
I created the dictionary to do it, but I don't know how to do the sum inside the dictionary because it's not a string.
To better understand what I want:
Quote: example : l + o + v + e = 54
list_alphabet = {} alpha = 'a' #first letter in alphabet for i in range(0,26): list_alphabet[alpha] = i +1 alpha = chr(ord(alpha)+1) print(list_alphabet) # for break word --> use split() ?
Never mind I resolved!!!
def wordsToMarks(word): res = 0 list_alphabet = {} alpha = 'a' #first letter in alphabet for i in range(0,26): list_alphabet[alpha] = i +1 alpha = chr(ord(alpha)+1) for letter in word: res += list_alphabet.get(letter,0) return res word = 'love' print(wordsToMarks(word))