Scrabble Score


#1



Exercise: scrabble score


When I execute this code it says: "Oops, try again. Your function fails on scrabble_score("xenophobia"). It returns "23" when it should return "24"."

Where's the error ?


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):
    #print word
    word_2 = word.lower()
    listaLetras = list(word_2)
    puntos = 0
    
    for c in score:
        if(c in listaLetras):
            puntos = puntos + score[c]
    print puntos
    return puntos
    
scrabble_score("xenophobia")


#2

Your method is counting the o only once. Use the list only as a lookup table, not an interable. Iterate only the word, which does not need to be cast to a list.


#4

Thank you !

I tried with this and it worked:


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):
    #print word
    word_2 = word.lower()
    listaLetras = list(word_2)
    puntos = 0
    
    for c in listaLetras:
        if(c in score):
            puntos = puntos + score[c]
    print puntos
    return puntos
    
scrabble_score("xenophobia")


#5

In the review process, see if you cannot simplify this using the word only, not a list. word is a local variable and its value will not be affected outside the function, so we can mutate it with no ill effect. That means we don't need a second variable.

word = word.lower()

This line,

is okay for ruling out none alpha characters but is not very explicit. The time to check the word is on entry into the function.

if not word.isalpha(): return False    # or return 0

Once we know the word is alpha, we can iterate over the letters.

for letter in word.lower():
    total += score[letter]

Since this is a utility function, we should not print before returning. Better to print the return value.

print (scrabble_score("xenophobia"))

#6

The print statements were only for checking if everything was running when I was writing the function, I just deleted them after I completed the exercise

And I don't understand what the ".isalpha()" function does there.
Why are you using it there ?


#7

It is not part of the expected solution, just an example of how to check if inputs contain only alphabet characters. In a scrabble program it would be feasible since there are no spaces, numbers or special characters in a word.


#8

Okay, I understand now.
Thanks a lot !

By the way, I have another question.
It's not related with the scrabble exercise, but with python itself:

Do you know whether it's possible or not to have installed both Python 2 and 3 on the same computer and in the same operative system ?


#9

In a virtual environment we can run as many different versions as we wish. They may not touch each other, however.

One exception is data structures. They may not be version dependent. This gives a vector by which one version may communicate with another.


#10

I am afraid this is beyond my knowledge.
What is a virtual environment?


#11

I'm not really one to discuss this with, but it is essentially running an OS inside an OS.


#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.