9. scrabble score


#1



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


Enter word: FACE
Traceback (most recent call last):
File "python", line 16, in
File "python", line 9, in scrabble_score
TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'


I'm really unsure why this error is coming up with a Unicode-related message. I've used these commands before and never had an issue with a Unicode error, and googling around a bit hasn't produced any insight into why THIS time, it's trying to make my input command Unicode instead of a normal string that I can manipulate.


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_score = 0
    word_in = str.lower(raw_input("Enter word: "))
    for letter in word_in:
        print letter
        word_score += score[letter]
        return word_score
        print word_score

print scrabble_score('')


#2

don't overwrite the function parameter

str.lower(), str is short for string, which is the string you want to convert to lowercase, in this case raw_input:

raw_input().lower()

if you want to do this, prompt the user outside the function and supply the user input as argument.


#4

Adding onto that, you should also note your indentation for return word_score. The way you're writing it currently will lead to the function returning the score of the first letter only.


#5

Yeah, I caught that afterwards. I have a lot of trouble remembering to correct the indentation on lines like that. Python's indentation is my nemesis. Thanks for the reminder!


#7

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