How to ensure repeated characters are accounted for in a for loop in python
<p>I'm working through the exercises in a textbook and I'm asked to create a numerology program that takes a name, assigns each letter a value based on ascending order in the alphabet and returns the sum of the letters. </p>

<p>The code I've written works fine for any name that doesn't repeat a letter. For example if the name is 'kayla', I know the if statement will stop at the first instance of 'a'. Is there a way within its current design to catch multiple letters or does it need to sort through the letters a different way?</p>

<pre><code>def main():

   alphabet = 'abcdefghijklmnopqrstuvwxyxz'
   user_name = raw_input('Please enter your name ')
   value = sum = 0

   for char in alphabet:
      value = value + 1
      if char in user_name:
          sum = sum + value

   print sum
</code></pre>

<p>A very simple way using list comprehension and the built-in <code>sum</code> function:</p>

<pre><code>   alphabet = 'abcdefghijklmnopqrstuvwxyxz'
   user_name = raw_input('Please enter your name ')
   user_name = user_name.lower().strip()
   value = sum([alphabet.index(c)+1 for c in user_name])
   print(value)
</code></pre>

python for-loop