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

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.

6 Likes

2 posts were split to a new topic: Passed, but Code Prints an Error When Run

2 posts were split to a new topic: Why Write a Loop Inside of a Loop?

3 posts were split to a new topic: Wrong return value

A post was merged into an existing topic: Wrong return value

Hi! Thanks a lot for your help. I’m stuck in this exercise. Somehow I tested on my laptop and it works but on the CodeAcademy console says this:

Your function fails on scrabble_score("pie"). It returns "9" when it should return "5".

This is the code I have:

def scrabble_score(word):
	word = raw_input("Insert your word: ")
	word = word.lower()
	total = 0
	for i in word:
		total += score[i]
	print total
	return total

Would you anyone please point me on the right direction? Thanks in advance. :slight_smile:

A utility function such as this one (look up and summation) would not normally have user input. We supply the function a word to evaluate in the argument.

scrabble_score("pie")    # user input in argument
2 Likes

Beautiful! Thanks a lot, @mtf; it worked instantly. :smiley:
I’m loving Python. Thanks a lot for this space.

1 Like

Hi

This is my code:

def scrabble_score(word):
  total = 0
  for i in word:
    total += score[i]
  return total
  print total

It gives error code “Your function fails on scrabble_score(“pie”). It returns “1” when it should return “5”.”

Would anyone point me in the right direction? Thanks

[quote=“staffsar, post:11, topic:339350, full:true”]
Hi

This is my code:

def scrabble_score(word):
  total = 0
  for i in word:
    total += score[i]
  return total
  print total

It gives error code “Your function fails on scrabble_score(“pie”). It returns “1” when it should return “5”.”

Would anyone point me in the right direction? Thanks
[/quote].
try this code
it uses for loop to iterate each character of the function scrabble_score while comparing the character to its corresponding score dictionary point.

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

  #for i in score:
  #print  score[i]
def scrabble_score(word):
  word = word.lower()
  total = 0
  for i in word:
    
    if i in score:
      #x = score[i] or use get method
      x = score.get(i)
      total += x 
    
  return total
print scrabble_score("guwtSEA")


    
def scrabble_score(word):
  total = 0
  for i in word:
    total += score[i]
  return total
  print total

Actually, the only problem with this one is that it doesn’t fulfill the instruction:

  • Your function should work even if the letters you get are uppercase, lowercase, or a mix.

(Also, the print statement is never reached, but that probably was just for testing.)

Hi,
I think the problem might be caused by the fact that your variable name (i) also occurs in some of the possible word inputs (e.g. pie) and therefore the code looks only for this one letter in the word and doesn’t count the scores for other letters.

Try maybe using char instead of i as below:

for char in word:
total += score[char]

There is no correlation between the name of a variable and what it references.

    i = 'i'
    print (i)    # i
    i = 'c'
    print (i)    # c

    for i in 'pie':
        print i
   '''
    p
    i
    e
   '''

I tried this with a user input. It seems to work.

def scrabble_score():
word = raw_input("Enter your word here: ").lower()
result = 0

for letter in word:
result += score[letter]
print result
return result

scrabble_score()

This turned out to be easier than I first thought it will be. Some learners forgot the instruction which says

  • Your function should work even if the letters you get are uppercase, lowercase, or a mix.

For me that was the highest priority to not forget about lol

My code works, but I am confused on something. The instructions said to include upper and lower case letters. How does setting the variable “word = word.lower()” make the code include upper case? It it a mathmatical thing where -2 * -2 = 4? Which is saying that the dictionary already includes lower case letters so if an upper case is typed, it’s included?

def scrabble_score(word):
  word = word.lower()
  result = 0
  for char in word:
    result += score[char]
  return result

print scrabble_score("MontePython")

it doesn’t, .lower() converts any uppercase character to lower case characters.

Ahh, makes perfect sense. Thank you!!!