Scrabble


#1



I have a little problem with my code, before i make it work, i wanted to know how to take index from dict
could you give me any advice please?


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


#2

dictionaries don't have indexes, you can get the value by using the key:

dictonaryName[keyName]

in this case, your key are letters (a, b, c and so on)


#3

I thought the score[l] means the same thing as dictonaryName[keyName] doesn't it?
i mean i wanted to make "score[l]" equal to points of each letter in word


#4

it does, so score[l] will get you the value of the letter

Then what is your question?


#5

thank you very much
it still returns 0
so i thought there's something wrong with score[l], now i know it's fine


#6

raw_input shouldn't overwrite the function parameter, place raw_input outside the function, store the result of raw_input in a variable, and pass the variable as argument when calling the function


#7

Thank you very much, i'll try it


#8

def scrabble_score(word):
    w = raw_input("insert word").lower()
    total = 0
    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}
    for l in w:
        if l in score:
            total += score[l]
            
    return total

it working good now, but there's error "Oops, try again. Your function fails on scrabble_score("pie"). It returns "0" when it should return "5"."
when i use print("pie") it returns 5


#9

raw_input is still inside your function overwriting the function parameter?

so, when the exercise calls the function with different strings to validate it works correctly, the string of the exercise gets overwritten by raw_input


#10

Thank you, it helped me alot


#11