9. Stuck on Scrabble Score


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/3?curriculum_id=4f89dab3d788890003000096


I've tried different variations of this code and i cant quite seem to get it to run. I know there is an easier way to do this but i want to know if this code can be modified to run if at all possible. Here's the code so far.


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):    
    mylist = []
    summ == []
    thesum = 0
    str.lower(word)
    for ch in word:
        mylst.append(ch)
    for chr in mylist:
        for chrt in abcdefghijklmnopqrstuvwxyz:
            score[chrt].append(summ)
    thesum == (sum(summ))
    return thesum
    print scrabble_score('Python')


#2

there are so many mistakes in this code, lets gets started:

summ == []

you want to create a variable, so you should use a single equal sign (two equal signs is compare), you need to store the result of the lowercase conversion into a variable:

word = word.lower()

this line:

for chrt in abcdefghijklmnopqrstuvwxyz:

i don't even know where to start, you should check if chr (not chrt) is in abc..yz (which is a string):

if chr in 'abcdefghijklmnopqrstuvwxyz':

this line:

score[chrt].append(summ)

is wrong. you use append the wrong way around:

summ.append(score[chr])

and again you should use chr, since that is the variable which holds the characters of which you want points.

this line:

thesum == (sum(summ))

you want to update thesum variable, so you should use a single equal sign.


#3

Add to the @stetim94 words:

And

print scrabble_score('Python')

needs to be out of the function.
At least not under the return statement. Because program never reach this line.


#4

That is a good point, well spotted :slight_smile:


#5

def scrabble_score(word):    
    mylist = []
    summ = []
    thesum = 0
    word = word.lower()
    for ch in word:
        mylst.append(ch)
    for chr in mylist:
        if chr in abcdefghijklmnopqrstuvwxyz:
            summ.append(score[chr])
    thesum = (sum(summ))
    return thesum
print scrabble_score('Python')

#6

this:

abcdefghijklmnopqrstuvwxyz

is a string, so you should use ':

'abcdefghijklmnopqrstuvwxyz'

this line:

mylst.append(ch)

you misspelled mylist


#7

Thanks! it finally worked. You have really been of much help. Thanks.