9/15 Scrabble Score


#1

Hi all,

I've been using a site (http://www.pythontutor.com/visualize.html#mode=edit) to check my code, as it provides a step by step view of what you've written. Very helpful! The issue today is that, after using that site to walk through the steps I'd written, I copied the text over here, and it's throwing the following error:

Oops, try again. Your function fails on scrabble_score("pie"). It returns "[3, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1, 1]" when it should return "5".

I should also mention that the code does in fact print 5, like it is supposed to. I don't understand what this error means, or why the right answers is returned with the error...

Here is my code. I looked at other solutions on here, and I realize now that this could be streamlined, but I am just happy that I finally wrote a whole piece of code without checking people's answers along the way! I was beginning to doubt my logic skills!

If anyone can see an error, please let me know. (Also, I wanted to include a screen shot, but I can't see how it's done with the menu that pops up!)

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}
         
tally = []

def scrabble_score(word):
    word = word.lower()
    for i in word:
        for key, value in score.items():
            if i == key:
                score.get(i)
                tally.append(value)
    return tally

scrabble_score('pie')

total_score = sum(tally)
print total_score

#2

You just have to see if it is in the list,

def scrabble_score(word):
    score_end = 0
    for letter in word
        score_end += score[letter]
    return score_end

#3

Thanks for the reply, but I'm looking for an answer to my question, not a different way to code what I've written. As I've said, I know there are cleaner ways to code this. It works outside of codecademy, so I'm trying to understand why it doesn't work here. Any ideas?


#4

Simply put,

score.get(i)
tally.append(value)

Only one of these lines are doing something. You have to remember when something returns a value if you don't capture it it does nothing at all. Also from the way your for loop is programmed you don't have any need for it any ways, when you use for key, value you get both the key and value which means you have all the information you need.

Also it's not about cleaner it about functionality, your code does roughly 26x the work mine does because you are looping though the list, and that is just for this list. If you program like this for a huge data structure you will ram out quick.

If you really want to looping through the dict of letters then do it like so,

def scrabble_score(word):
    tally = [] # Using data outside a function is viable but not recommended
    word = word.lower()
    for i in word:
        for key, value in score.items():
            if i == key:
                 # score.get(i) Yo do not need this line it does nothing for your code
                tally.append(value)
    return sum(tally)

#5
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}
     
def scrabble_score(word):
    total = 0
    for char in word.lower():
        total += score[char]
    
    return total

#6

OH! I was wondering about all the looping. I'll take a closer look at your way to see if I can figure out what it all means.

This makes perfect sense. Thank you for explaining it to me in a clear way, and for commenting on the parts that were not done in the most functional way!

(I'm still not sure why my code displayed the correct answer but threw an error, but I guess that will remain a mystery.)


#8

How to put a raw input there??
i triend with this line
word = raw_input("Enter a word: ")
but its not working,it doesnt use the word, doest print the score just returns "none"

word = raw_input("Enter a word: ")

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


#9

Hey Zeziba,

How does 'for letter in word' see if the letter is in the list. Also where is the lookup of the value that is assigned to the key in what you're doing?

Thanks!


#10

For loops inherently loop over what you give them, if they are not iterateable then you will get an error.

That being the case I just went over the word and then matched the letter in the score dict that we have.


#11

Hi, can i check for the score[i], which part of the python syntax is it from? thx :slight_smile:


#15

It isn't THAT simple. You need to check for the upper-case letters as well. Using the lower function, we get:

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

Just correcting the solution :wink:


#16