How can I access a dictionary key value inside of a loop?


#1

Question

How can I access a dictionary key value inside of a loop?

Answer

One approach to writing our scrabble_score() function involves using a for each loop to access the dictionary value of each character in the given word.
Since we are given a word as a string, we can iterate through it using a for each loop, where each time the loop loops, the loop variable becomes the next character in the string.
We’re also given a dictionary where each key is an alphabetical letter corresponding with a value. Convenient!
Inside of the loop, we can access the value stored in a key by writing dictionary_name[key_name]. That will give us the value of the character, which we can add to some total and return.


FAQ: Learn Python - Practice Makes Perfect - scrabble_score