9.scrabble_socre


#1

I managed to finish the code, but I still don't understand well why this code doesn't work. please help!!


#2

for key in score:

We are supposed to check the score of "each alphabet in the word" that we are giving the function. So what are we supposed to do, then?


#3

your code can only count score for 1 word, but cannot count string with more than 1 word:

You can try this:
def scrabble_score(word):
total = 0
count = len(word)
i = 0

while count > 0:
    for key in score:
        if word[i].lower() == key:
            total += score[key]
    count -= 1
    i += 1
return total

print scrabble_score("pie")


#4


#5

Try this method.

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 i in word.lower():
        total += score[i]
    return total
print scrabble_score('Python')

#7

Hey, I am trying like this. I am not ale to get the answer we are expecting. I understand that your converting the whole word in to lower case. Kindly let me know

def scrabble_score(word):
    final = []
    result = 0
    for i in word:
        if i.lower() in score:
            final.append(score[i])
    for s in final:
        result += int(s)

    print(final)
    print(result)

scrabble_score("Python")

#8

why dont you do similar to this.
convert the word to lower case before doing anything.

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

#9

Hello there,

I am so happy that I challenged myself to write the code without looking at the forum and I did it. Try my code below:

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("Please enter a word: ")
def scrabble_score(word):
    if word.isalpha():
        word = word.lower()
    else:
        print "Please enter a word with no spaces, punctuation or digits"
    total = 0 
    for letter in word:
        total = total + score[letter]
    return total  
print scrabble_score(word)

:muscle:
Thank you