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

I basically have to take the input word and count its score depending on the dictionary.

Elazar
  • 20,415
  • 4
  • 46
  • 67
praxmon
  • 5,009
  • 22
  • 74
  • 121
  • You can do the loop much more easily: `count = sum(score[char] for char in word)`. – Karl Knechtel Jun 23 '13 at 13:35
  • 1
    Note that you can express `scrabble_score(word)` as simply `sum(score[char] for char in word.lower())` – Eric Jun 23 '13 at 13:35
  • Duplicate of [Why doesn't calling a Python string method do anything unless you assign its output?](http://stackoverflow.com/questions/9189172/why-doesnt-calling-a-python-string-method-do-anything-unless-you-assign-its-out) – smci Mar 02 '15 at 10:00

3 Answers3

8

This modified code will work:

def scrabble_score(word):
    count=0
    word = word.lower() #assign the result of word.lower() to word

word.lower() returns the modified word, it doesn't modify the string inplace. Strings are immutable in Python. The fact that .lower() returns a string is defined as such:

>>> help(str.lower)
Help on method_descriptor:

lower(...)
    S.lower() -> string

    Return a copy of the string S converted to lowercase.
HennyH
  • 7,794
  • 2
  • 29
  • 39
5

str.lower() returns a copy of the string - it does not change the original string. Try this:

word = word.lower()
grc
  • 22,885
  • 5
  • 42
  • 63
4

The scrabble_score function can be expressed more simply like this:

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

In Python, the idiomatic way to express an iteration is to use generator expressions (as in the above code) or list comprehensions.

Regarding your current problem, as has been pointed in the other answers, the lower() method (and all other string methods for that matter) don't modify the string in place, so you have to either reassign the returned value or use it immediately, as shown in my answer.

Óscar López
  • 232,561
  • 37
  • 312
  • 386