1
word   = input('Enter word: ')
SCORES = {
  'a' : 1, 'b' : 3, 'c' : 3 , 'd' : 2, 'e' : 1, 'f' : 4, 'g' : 2,
  'h' : 4, 'i' : 1, 'j' : 8 , 'k' : 5, 'l' : 1, 'm' : 3, 'n' : 1,
  'o' : 1, 'p' : 3, 'q' : 10, 'r' : 1, 's' : 1, 't' : 1, 'u' : 1,
  'v' : 4, 'w' : 4, 'x' : 8 , 'y' : 4, 'z' : 10,
}

for letter in word:
  result = SCORES[letter]

This is what I have so far:

Enter word: quiz
10
1
1
10

I want it so that I can add the values together to get 22. How can I do this?

2 Answers 2

6

Try this, it's the idiomatic solution:

sum(SCORES[letter] for letter in word)

In Python, whenever possible we prefer to use list comprehensions and/or generator expressions instead of explicit looping. They're shorter, simpler and generally faster than explicit loops!

Sign up to request clarification or add additional context in comments.

1 Comment

might be shorter and faster but not everything can be done in a list comprehension or generator expression.
4
result = 0 # create variable outside the loop
for letter in word:   
   result += SCORES[letter] # add score for each letter 
print(result)   # print total

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.