Scrable score


#1

Hello guys,
I don't know why this does not work (it is ugly i know).
i got "Your function fails on scrabble_score("pie"). It returns "51" when it should return "5"."
while it prints me 5, but return 51

Thanks in advance!

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}
         
nana = []
nono = []
nunu = []
def scrabble_score(word):
    nini = 0
    for i in word:
         nana.append(i)
    for i in nana:
        nono.append(i.lower())
    for i in nono:
        nini= nini + score[i]
    print nini
    return nini
scrabble_score ("Pie")

#2

If you add a second function call, you will see the lists accumulate data because the defining the lists doesn't happen inside the function


#3

■■■■, i'm sadly stupid.
Thanks a lot!


#4

no, you are not. Programming is just very difficult

See if you can reduce your code base, you only a single loop. You can simply convert word to lower first (before the loop) and then loop over the lowercase letters and add them to score directly


#5

like this?

def scrabble_score(word):
    nana = []
    nini = 0
    for i in word:
         nana.append(i.lower())
    for i in nana:
        nini= nini + score[i]
    print nini
    return nini

#6

if you want, or you just call .lower() on word and store this in a variable. Now you have a variable containing the lowercase string

.lower() can convert a whole string at once, it doesn't have to be done one letter at a time


#7

ahhhhhh thanks yeah it looks much better like this!

def scrabble_score(word):
    nana = word.lower()
    nini = 0
    for i in nana:
        nini= nini + score[i]
    print nini
    return nini
scrabble_score ("pie")

#8

yes, it does absolutely. Well done :slight_smile:


#9

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