You will be better off using the dictionary data structure.
EDIT: This is not my source code, but instead the code updated on DSM lines.
import string num_vals = [.0817, .0149, .0278, .0425, .1270, .0223, .0202, .0609, .0697 , .0015, .0077, .0402, .0241, .0675, .0751, .0193, .0009, .0599, .0633, .0906, .0276, .0098, .0236, .0015, .0197, .0007] letterGoodness = {letter : value for letter,value in map(None, string.ascii_uppercase, num_vals)} def goodness(message): string_goodness = 0 for letter in message: letter = letter.upper() if letter in letterGoodness.keys(): string_goodness += letterGoodness[letter] return string_goodness print goodness("I eat")
Using the test case you provided:
print goodness("I eat")
outputs the result:
.369
One note - creating a dictionary, as done here, is required for Python 2.7+. The same thing can be done in Python 2.6+ with the dict() constructor.
source share