Can't figure this one out... 9. scrabble_score


#1

I thought this one would be easy. I came up with the following code:

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):
    word.lower()
    total = 0
    for char in word:
        if char == "a" or "e" or "i" or "l" or "o" or "n" or "s" or "r" or "u" or "t":
            total = total + 1
        elif char == "d" or "g":
            total = total + 2
        elif char == "c" or "b" or "m" or "p":
            total = total + 3
        elif char == "f" or "h" or "w" or "v" or "y":
            total = total + 4
        elif char == "k":
            total = total + 5
        elif char == "j" or "x":
            total = total + 8
        elif char == "q" or "z":
            total = total + 10
    print total

I have two things to ask about this. First, surely there is an easier way! All this seems unnecessary to me, but I can't figure out what I should use instead. Second, why doesn't it work?


#2

well, if you want to do a elif statement:

elif char == "q" or "z":

If you use or, you should also tell where the or statement should compare too:

elif char == "q" or  char == "z":

Yes, there are more efficient solutions, you could simple use the dictionary to add the score:

total += score[i]

i is your letter, so score[i] will be the number (score) of your letter, and add it to your total

for next time, use one of the two following options to make your code/indent is visible:

select your code and press ctrl + shift + c (or cmd + shift + c if you use a mac)

if this instructions are unclear, you can also insert 3 backticks before and after your code, like so:

```
<p>visible</p>
```

the backtick is located above the tab key on your keyboard


#3

Well, I want to use code that I understand. And since I'm not entirely sure how to use the dictionary, I came up with the elif thing. I guess I don't know as much about Python as I thought, because I've had to come here for almost every single exercise in the Practice Makes Perfect section. :frowning::confused::disappointed:


#4

That is the whole point of this exercises (practice makes perfect), try to improve your code on the exercises, increasing your understanding of dictionary's.


#5

I see. While you're here, also could you tell me what's wrong with this code?

 def purify(lisk):
     lisk_purified = []
     for num in lisk:
         if num % 2 == 0:
             lisk_purified.append(num)
     print lisk_purified

#6

What is the error message you receive? You should use return in your function, not print


#8

You can read the details from the dictionary using this
scr = scr + score[char]


#11

Try this it worked for me:

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):
    point=0
    for i in word.lower():
        if i in score:
            point=point+score[i]
    return point

#12

Yes I just figured that out a few minutes ago.