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.
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.
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.
[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")
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.
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")
I don’t get one thing in this answer: where does it reaches the list? like, for letter in word is iterating through the word, not the list. how can the result variable reach it there?
I am also updating my answer with this another code, that doesn’t have user input, but I am still curious how it gets to analyze the word through the list as it doesn’t seem to have a for/other iterator to analyze the word in the list. Is the secret on score[char]?