Scrabble_score - Unidentified error!


#1



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


I have written something which to all intents and purposes, I think should work. Infuriatingly, I'm getting this error message:
"Oops, try again. Did you create a function called scrabble_score? Your code threw a "global name 'string' is not defined" error."
Weirdly, if I comment out everything after def scrabble_score(word): (and then just add a return True to that function), it runs perfectly. Telling me that I get the exercise wrong because it returns True.. which is expected.


Why is it, that def scrabble_score(word): throws an error with my function below it, but not if I just pop return True there?


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):
    mark = 0
    word = string.lower(word)
    for l in word:
        mark = mark + score[l]
    return score


#2

to convert word to lowercase, use word.lower(), word is your string

seems return True is a corner case to pass the exercise


#3

Sir,

I have replaced my script with word.lower() as suggested. Many thanks.

Unfortunately, I am still encoutering my issue of:

"Oops, try again. Did you create a function called scrabble_score? Your code threw a "global name 'string' is not defined" error."


#4

can i see an updated version of your code? if you did what i said, you shouldn't have string anymore


#5

You re meant to return mark and not score


#6

Gents,

Updated script for reference, This script now works!

Thank you both for you help!

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

#7

Your code is good to go. Are you still having problem with it?


#8

i think this a pretty big give away?


#9

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