Add variable of same index and add to list
<p>So I want to add a variable of the same index of another list and add that variable to a total. In this case I am trying to make the points be equal to the letters in a game of scrabble.</p>

<p><code>letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']</code> </p>

<p><code>point_values = [1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10]</code></p>

<p>So I would want <code>a</code> to equal to 1 and <code>c</code> to equal to 3. </p>

<p>Say I have a <code>rack = ['c','a','t']</code><br>
How could I make <code>'rack'</code> == <code>points = 5</code>?</p>

<p>Here is my preexisting code: It gives a <code>Out[1]: 87</code> if I said <code>'c'</code> and for every single letter.</p>

<pre><code>import random

def load_words():
    """
    Returns a list of valid words. Words are strings of lowercase letters.
    """
    # inFile: file
    inFile = open('words.txt', 'r')
    # line: string
    line = inFile.readline()
    # wordlist: list of strings
    wordlist = line.split()
    return wordlist
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
point_values = [1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10]
bag_of_letters = []
for x in range(26):
    for i in range(letter_count[x]):
        bag_of_letters.append(letters[x])
rack  = []
for i in range(7):
    rack.append(bag_of_letters.pop(random.randint(0,len(bag_of_letters)-1)))
print (rack)
points = 0
total_points = 0
round_count = 10
letterlst = []
while(round_count &gt;= 0):
    word = input("GIMME A LETTER TO COUNT THE SCORE OF: ")
    for i in word:
        letterlst += i
    for let in word:
        for letter in letters:
            for word_rack in rack:
                if let == word_rack:
                    points += point_values[letters.index(letter)]
    total_points += points
    if round_count == 0:
        print("You have gotten",total_points,"points at the end of your round")
    print(points,"points this round")
    points = 0
    round_count -= 1
</code></pre>

<p>Below is the simplified version of your code:</p>

<pre><code>&gt;&gt;&gt; letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
&gt;&gt;&gt; point_values = [1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10]

&gt;&gt;&gt; my_string = 'cat'
&gt;&gt;&gt; sum(point_values[letters.index(c)] for c in my_string)
5
</code></pre>

<p><strong>Explanation</strong>: Iterate over each character in the string. Find the index of the char in <code>letters</code> list. Get the value of that index from <code>point_values</code>. Sum all the values. All this is been done in last line of code. Python is <em>MAGICAL</em>. Right? :)</p>

python indexing