2

I'm very new to dictionaries and I'm trying to implement a simple game of scraable in python that returns the score I get for each word I input. However I`m not to familiar with dictionaries and I want to understand what's wrong with my code.

I've created a dictionary where every letter has its own score.

points = {'A':1, 'B':3, 'C':3, 'D':2, 'E':1, 'F':4, 'G':2,
'H':4, 'I':1, 'J':8, 'K':5, 'L':1, 'M':3, 'N':1,
'O':1, 'P':3, 'Q':10, 'R':1, 'S':1, 'T':1, 'U':1,
'V':4, 'W':4, 'X':8, 'Y':4, 'Z':10}

def scraable():
    total_score=0
    word=input('Digit a word\n')
    for i in word:
        total_score=total_score+points[i]
    return total_score
print(scraable())

It gives me a key error 'p' and I don`t know what type of error is this

4
  • 3
    If your input is in lower case you should convert it to upper case, since that is what your dictionary uses. Commented May 6, 2019 at 10:06
  • 1
    The points dict has only upper case letters so it seems that you would need to change the calculation of the score to: 'total_score=total_score+points[i.upper()]'? Commented May 6, 2019 at 10:06
  • 2
    as @khelwood mentions, it's probably just the fact that your dictionary keys are in upper case but you inputted a lower case. try adding i.upper() in ...+points[i]. Thanks @glhr Commented May 6, 2019 at 10:07
  • @zero i.upper() not i.lower(), the keys are in uppercase. Commented May 6, 2019 at 10:07

2 Answers 2

1

The most pythonic way would be to use a comprehension and sum:

points = {'A':1, 'B':3, 'C':3, 'D':2, 'E':1, 'F':4, 'G':2,
'H':4, 'I':1, 'J':8, 'K':5, 'L':1, 'M':3, 'N':1,
'O':1, 'P':3, 'Q':10, 'R':1, 'S':1, 'T':1, 'U':1,
'V':4, 'W':4, 'X':8, 'Y':4, 'Z':10}

def scraable():
    word=input('Digit a word\n')
    return sum(points.get(l.upper(), 0) for l in  word)

print(scraable())

We use dict.get to avoid error if something is not in the dictionary getting a default value of 0 points for it. Also, we use str.upper because in the dict all keys are in uppercase, so in case the input is in lowercase it will fail ("W" != "w").

Sign up to request clarification or add additional context in comments.

5 Comments

a simple question.
Imagine i have the following code v=[] def creat_list(): n=int(input('Number of elements in list\n')) for i in range(0,n): x=int(input('Digit element\n')) v.append(x) return v print(creat_list()) how do i do a list compreension of this.
return [int(input("Digit element\n")) for i in range(0, int(input('Number of elements in list\n')))]
and in that list comprehension where do you append the elements?
You are taking n input elements, surrounding the x for x in xs expresion by [] it creates a list with those elements.
1

As said in the comments, put your letters in upper case as follow :

for i in word:
  total_score=total_score+points[i.upper()]

Comments

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.