0

This code is for adding score based on the letters containing in the word given as argument:

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 = word.lower()
  n=0
  scorer=0
  while n<=len(word):
    scorer = scorer + score[word[n]]
    n+=1
  return scorer

ignore any other syntax error that I might have made..

4
  • 3
    What is your question? Commented Feb 12, 2019 at 14:47
  • the code is giving error in output Commented Feb 12, 2019 at 14:55
  • Please be more specific than "giving error" – what error and on which line? Commented Feb 12, 2019 at 14:59
  • Just remove = sign :P Commented Feb 12, 2019 at 14:59

3 Answers 3

3

Iterate directly over the output of word.lower(), rather than indices. Also, you can use the sum function to compute the sum of all the dictionary lookups.

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

A less concise version, sticking with the spirit of your original code, would still iterate over word directly.

def scrabble_score(word):
    scorer = 0
    for c in word.lower():
        scorer = scorer + score[c]  # or scorer += score[c]
    return scorer
Sign up to request clarification or add additional context in comments.

Comments

0

while n<=len(word): will throw Index out of bounds

you need while n<len(word)

The working copy of your existing function

def scrabble_score(word):
    word = word.lower()
    n=0
    scorer=0
    while n<len(word):
        scorer += score[word[n]]
        n+=1
    return scorer

As others have pointed out a much cleaner approach would be to iterate over the characters of the word rather than indices

def scrabble_score(word):
    word = word.lower()
    scorer=0
    for char in word:
        scorer += score[char]
    return scorer

Comments

0

Your code is correct. However, two style-related things

In python, strings are iterables of characters, so

scorer = 0

for letter in word.lower():
    scorer += score[letter]

Even, better, you can use a list comprehension

scorer = sum([score[letter] for letter in word.lower()])

6 Comments

That's not a list comprehension; it's just a generator expression. sum([score[letter] for letter in word.lower()]) would make use of a list comprehension.
@chepner: correct, I omitted the brackets while typing.
There's no reason to use a list comprehension, though. sum just needs an iterable, and generator is sufficient (and does not require all the individual scores be computed before summing, saving memory).
@chepner: I know, but mentioning generator expressions to someone seeing the language for the first time seems a bit too much.
Why? They're very useful, and not that hard to understand.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.