9/15 scrabble_score


#1

Here is my 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 = word.lower()
total = 0
for char in word:
for key in score:
total = total + score[char]
return total
"""
it gives me the following error:
Oops, try again. Your function fails on scrabble_score("pie"). It returns "130" when it should return "5".
where is the problem?


#3

My 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}

word = raw_input("Enter a word: ")
def scrabble_score(word):
word = word.lower()
scrabble = 0
for char in word:
scrabble = scrabble + score[char]
return scrabble


#4

Look the first for loop starts in case of the input "pie" with char = "p" and will switch to char = "i" (which is the next element or letter in the word) if the iteration in the 2nd loop over the dictionary called score is over, in this case after {z : 10}.

There are 26 letters (or keys) in score from a to z. In your case:

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

Char will be 'p' in the first for-loop and will wait for 26 iteration cycles of the 2nd for-loop.

The value for pie is 5 ( {"p" : 3, "i":1,"e" = 1}) -- so: 3 + 1 + 1 = 5
What you are doing is (3 x 26) + (1 x 26) + (1 x 26) = 130

So you have to add to total only if key == char and not every iteration cycle... I hope that you can get it.

def scrabble_score(word):
    word = word.lower()
    total = 0
    for char in word:
        for key in score:
            # You need an if-statement here to add to total only if  **char** and **key** are equal
            total = total + score[char]
    return total

#5

Thank you dude you are the real MVP!


#6

There is no need for the second for loop. The first loop is enough. The iterator "char" matches the letter score with each letter in the input word.

Try this running the for loop outside the function to see what happens:

for char in word:
    print char
    print score[char]

Now that you know how the for loop behaves, put it into the function like so:

def scrabble_score(word):
    # make sure all letters are lower case:
    word = word.lower()
    # not asked for but  I found it useful to strip all whitespaces in a word;
    word = word.replace (" ", "")
    # set the total score = 0
    total = 0
    # start the for loop:
    for char in word:
        # total = total + score[char] works too.
        total += score[char]
    # return the score
    return total



 # try out the function on different stuff:
print scrabble_score("abalone")
print scrabble_score("Orcinusorca")
print scrabble_score("SOMETHingOutOftheOrdinary")
print scrabble_score("Alright this is a phrase and it it just silly but it works")

#7

I know. I'm not sure about the exact exercise guidelines whether to train to use cascaded for-loops or not and this was just a reply to his solution. But in this exercise you define a function to sum up the scrabble score therefore according to the exercise whitespaces and sentences aren't allowed as inputs but a single word as input. (Rely on correct inputs)


#8

You made it really easy and that line to strip all whitespaces is very cool, indeed. Thanks.


#10

My way.


#11

Try this.

def scrabble_score(word):
a=0
for i in word:
i=i.lower()
if i in score:
a+=score[i]

return a

#12

This was a very helpful walkthrough. Thanks for the help!


#13

Why is this code failing?

def scrabble_score(word):
if word == str:
x += score(int(key))
return x
else:
return 0
print scrabble_score("cat")


#14

perfect,..explanation


#16

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

scrabble_score("Helix")


#17

hi

can you explain here why is scrabble=0 etc?
I got in the console"none"
not some points...so is it wrong I think


#18

def scrabble_score(word):
word=word.lower()
s=[]
for i in word:
s.append(score[i])
return sum(s)


#19

Could someone explain why we don't need a second for loop to iterate through the keys in score dictionary to check if char is equal to that key, before assigning the score value?


#21

There's no need for another for loop to iterate through the score list because every letter in the alphabet has been given a numerical value, so there is no need to verify that the letter you're checking is contained within the score list. You'd simply need to add the value of the letter by adding (+=) score[letter].


#23