In your case, It is better to use a dictionary which defines all the characters and there value. The string
library provides an easier way to do this. Using string.ascii_lowercase
within a dict comprehension you can populate your dictionary mapping as such.
>>> import string
>>> wordmap = {x:y for x,y in zip(string.ascii_lowercase,range(1,27))}
>>> wordmap
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'g': 7, 'f': 6, 'i': 9, 'h': 8, 'k': 11, 'j': 10, 'm': 13, 'l': 12, 'o': 15, 'n': 14, 'q': 17, 'p': 16, 's': 19, 'r': 18, 'u': 21, 't': 20, 'w': 23, 'v': 22, 'y': 25, 'x': 24, 'z': 26}
Now you can easily map this to your output. First we take the input
>>> word = str(raw_input("Enter a word: ").lower())
Enter a word: apple
>>> values = []
Now we just loop through the input word and append the values to an empty list. We append the values because we also need to find the sum of the values.
>>> for i in word:
... print "{}".format(wordmap[i])
... values.append(wordmap[i])
...
1
16
16
12
5
You can finally use the sum
function to output the sum total of the values.
>>> sum(values)
50