9. scrabble_score


#1
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):
    raw_input(word)
    for i in word:
        if i == score['a','e','i']:        
            return i
        elif i == score ["l","o","n"]:
            return i
        elif i == score ['t','s','r','u']:
            return i
        #letters have 1 score 
        elif i == score["d","g"]:
            return i
        #letters have 2 score 
        elif i == score ["c","b","m"]:
            return i
        #letters have 3 score
        elif i == score["f","h","w","v","y"]:
            return i
        #letters have 4 score
        elif i == score ["k"]:
            return i
        #letters have 5 score
        elif i == score ["j","x"]:
            return i
        #letters have 8 score 
        elif i == score ["q","z"]:
            return i
        #letters have 10 score
    sum(i)
    return i

i know my code is stupid :(, but Can you guys help me on this section by the way i think.
Is my thinking possible for this exercise?


#2

I hope this is what you wanted to write.

If you have any questions or if my code is not consistent with your idea just let me know :smile:

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():
    print "Please input a word: "
    word = raw_input()
    word = word.lower()
    result = 0
    for i in word:
        if i in ['a','e','i', 'l', 'o', 'n', 's', 'r', 'u', 't']:        
            result += 1 
        elif i in ['d', 'g']:
            result += 2 
        elif i in ['c', 'b', 'm', 'p']:
            result += 3
        elif i in ['f', 'h', 'w', 'v', 'y']:
            result += 4
        elif i in ['k']:
            result += 5
        elif i in ['j', 'x']:
            result += 8 
        else:
            result += 10
    return result

print scrabble_score()

#3

I think factoradic has the best example based on your model, and it works. In fact he just helped me with this very problem :wink: .

Your code isn't stupid, in fact it's too smart --- you're doing all the tedious work the computer should be doing for you :smile: ! One thing I'm glad I recently discovered in working with lists & dictionaries is the double 'for x in y' statements. When I use those, like...

for letter in word:
        for key in score:

...it lets the computer quickly loop through both the word itself and the key/value pairs, so even if score was a huge data set I wouldn't have to pick out the values by hand. Good luck!

def scrabble_score(word):
    lst = []
    word = word.lower()
    for letter in word:
        for key in score:
            if letter == key:
                lst.append(score[key])
            else:
                continue
    return sum(lst)
    
print scrabble_score('pie')

#4

In this case you can make it even more simple:

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}
         

entry = str(raw_input("What's your word?"))

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

print scrabble_score(entry)

#5

Wow! That's brilliant! Thanks for the tip. :grinning: It seems almost obvious after the fact (like 'Why didn't I think of that?') This is the great part of being in a forum.


#6

def scrabble_score(word):

A = list(word.lower())
total = 0
for i in A:
    total = total+score[i]
return total

#7

I pretty much did the same thing, but 1 less line (use .lower in the for loop)

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

#8

I don't know how this thread turned into list of solutions, but I will just go with the flow:

def scrabble_score(word):
    return sum([score[c.lower()] for c in word])

#9

i wrote the same program.This ain't working for me


#10

At least post what the error was! I can't troubleshoot "This ain't working for me". :smile:

Did you just cut-and-paste or did you re-type?


#11

no i did it by my own it wasnt working for me


#12

Here was my code,

def scrabble_score(word):
    points = 0
    word = word.lower()
    for letters in word:
        points += score[letters]
    return points

Converting the word to a lowercase made it easier since its a non-factor when dealing with points. Also applying the lower method doesn't affect special characters.

eg. 'A' is graded the same as 'a'


#13

My solution:

def scrabble_score(word):
sum=0
for i in word:
sum+=score[str.lower(i)]
return sum

Note: @hila5 - please make sure that next time you post a solution of any sort, you also explain how it works. It's counter productive to just post answers without explanations.


#15

I came up with this.

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
    userInput = []
    for l in word:
        userInput.append(l.lower())
    for index, item in enumerate(userInput):
        if item in score:
            total = total + score[item]
    return total

print scrabble_score('DuNe')

I've used the first loop to make a list out of the userInput, also converted the input to lower-case.
Second loop to compare the userInput list with the score list (score list on top), then used an if statement to see if the letter exists, if the letters exists it'll add up the value of the letters and store it into the total, then return the total


#16

Because my memory is fuzzy, I'm more intuitive than rote learned. Looking at your solution and taking a run at simplifying it, I sketched the following...

def add(word):
    return sum([score[x.lower()] for x in word])

The score dictionary is assumed present.

add('Xylophone')      # 24
add('xylophone')      # 24

Then I scroll up the page to @factoradic's post, and lo and behold! When we look for the simplest solution, we often find it. When we get logic-stressed around a problem, the solution begins to look farther away that it actually is. Code bloat is the result. Just saying...


#17

for some reason it returns only the value of the first character for me.


#18

Yes, I think this (from both of you) is the best answer. As I get more into programming I love to see how the logic can be condensed from an almost literal 'step-by-step' process to something more elegant and efficient.

Another algorithmic expression to add to the bag o' tricks. Thanks!


#20

The range function in your for loop is wrongly used. You'd like to iterate over actual letters in the input string. If you use range(len(word)), you will end up iterate over a range of numbers. Following is my code, hope it will help you!

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

#21

fantastic code :grin: ..very clever..thanks


#24

Hi,

Your code is very useful for me.
I just leant a new experience.

Thank you,