def scrabble_score(word):
word = word.lower()
total = 0
for letter in word:
for leter in score:
if letter == leter:
total = total + score[leter]
return total
The above uses the inner loop to confirm that letter is a character in score, and if not it gets ignored. There are other ways to do this but that would require explanation and may introduce new concepts not yet taught.
In a perfect world, we would not need to do this extra step on the assumption that non-alpha characters are not passed to the function. The shorter version posted above some time ago is an example of a function that makes this assumption. At its core it does what we want it to do, but will trip up if non-alpha characters are given as inputs. Either example is sufficient to pass this exercise since the test values contain only alpha.
Your explanation was really helpful thanks! I was also wondering this same thing.
My code is below and is really similar to the original post. I keep getting an error when I try to print at the end to test the code. When I remove the print statement it runs. Any help?
def scrabble_score(word):
#this function will find the scrabble score for a given word using the provided dictionary
word=word.lower
word_score = 0
for char in word:
word_score =word_score + score[char]
return word_score
print (scrabble_score("pizza"))
Traceback (most recent call last):
File “python”, line 15, in
File “python”, line 11, in scrabble_score TypeError: ‘builtin_function_or_method’ object is not iterable
So it just won’t call the function in the print statement? It runs when I take the print statement out, and will let me move to the next lesson. I’m still kind of confused.
What you should be iterating through is a string. You have an error message saying you’re attempting to iterate through some function or method. Your error message also tells you where this happens, and since you only do iteration in one place you should be able to figure out where from that as well. From there you can can consider where you got that function or method from and/or ask yourself where your string should have come from since that’s what you should have been iterating through
If pure reasoning around your code and error message isn’t doing the trick, you can always insert prints in your code to write out what is going on. You’d probably want to write out the things that you rely on, or whatever information you want to know really.
Your error message provides you with something to investigate, that’s a very good thing. You’ll be able to locate and fix the problem by comparing what is happening (use prints to find out) to what should be happening (you should have already decided on this before you started writing code).
This was my code for the problem, which gets the error "Your function fails on scrabble_score(“pie”). It returns “None” when it should return “5"” despite returning the correct total score:
def scrabble_score(word):
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}
word = word.lower()
total = 0
for char in word:
if char in score.keys():
total += score[char]
print total
scrabble_score("pie") # prints 5
What did I do wrong? I tested it out with print, and it prints the correct letters out, and also shows me the test values the site uses (“test”,“abc”,“pie”) which also return the correct scores.
The score dictionary can be outside of the function, though it’s not doing any harm where it is. Only thing is it cannot be accessed from anywhere else so doesn’t serve a very universal purpose locked away inside a function.
def scrabble_score(word):
word = word.lower()
total = 0
for letter in word:
for leter in score:
if letter == leter:
total = total + score[leter]
return total
Second thing to look for is complexity. Do we need a nested loop?
score is a dictionary. The keys are unique. That makes it an ideal look up table. Give it the key, and it returns the corresponding, or associated value. Using .get() we can set a default return value rather than raising a KeyError.