Dictionarties


# scores

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

#input

words = input("Enter the word: ")

#keep track of score

sum = 0

#Find the key from the dictionary and add the number

for letter in words:

    if letter in scrabble[key]:

        sum = sum + scrabble[key]

print(sum)

I am trying to calculate the score of a scrabble word, I am trying to access each value but it is not working, telling me that key is not defined

Were is key defined? I don’t see that you used the name until line 18:

    if letter in scrabble[key]:

Currently it appears you are using a variable name that doesn’t exist, though I don’t think that’s what you were trying to do. Perhaps you meant to use an attribute?

1 Like

Are keys supposed to be defined for dictionaries? I don’t get what you mean by attribute, am trying to get the value using the key and add it to the score of the player

Thanks

As @8-bit-gaming has pointed out, you’re attempting to do this:

if letter in scrabble[key]

Python will attempt to resolve key to an entity of the same name, most likely a variable. Your program contains no variable named key so it fails with an error.

If, perhaps, you simply want to know whether a certain item exists in all defined keys of a given dict object then perhaps there is something which the dict object provides which would assist with that?

2 Likes

I am trying to do this

some of the things in the docs l find complicated, like they are talking of classes, subclasses

Here key is a name that needs to be defined. A dictionary has a set of key/value pairs.

dictionary = {1: "a", 2: "b", 3: "c"}

# you can obtain one of the values in the dictionary by using a key

print(dictionary[1])
# above 1 is the key
# You could also set a variable to hold the value of a key

key = 1
print(dictionary[key])
2 Likes

In my code l don’t want to specify, l want it to search through all my keys and if similar give me the value

For this you may want to use the .keys() method, which is what I thought you were doing earlier (it was a mistake on my part to refer to it as an attribute).

key = input()
dictionary = {"a": 1, "b": 2, "c": 3}

if key in dictionary.keys():
  # do something here
2 Likes

# scores

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

#input

words = input("Enter the word: ").upper()

#keep track of score

sum = 0

#Find the key from the dictionary and add the number

for letter in words:

    if letter in scrabble.keys():

        result = scrabble.keys()

        sum = sum + result

print(sum)

This is what l did and this is the error

PS C:\Users\simmy.vscode> & C:/Users/simmy/AppData/Local/Programs/Python/Python39-32/python.exe c:/Users/simmy/.vscode/test3.py
Enter the word: hi
Traceback (most recent call last):
File “c:\Users\simmy.vscode\test3.py”, line 15, in
sum = sum + result
TypeError: unsupported operand type(s) for +: ‘int’ and ‘dict_keys’

This is because of this line:

result = scrabble.keys()

You don’t want to add the dictionary keys (dict_keys) object to the result, you want the score of the letter.

To do that, you need to retrieve the value that the appropriate key refers to (which is what you were attempting to do earlier).

1 Like

even if l remove this l am getting the same error, this l did after getting the error


# scores

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

#input

words = input("Enter the word: ").upper()

#keep track of score

sum = 0

#Find the key from the dictionary and add the number

for letter in words:

    if letter in scrabble.keys():

        sum = sum + scrabble.keys()

print(sum)

Which step are you at in the Scrabble project?

I’m assuming that the code above what you’ve pasted shows that you have two lists, letters & points, then you used a list comprehension and .zip() to map the letter to the point value, combine them, to create a dictionary, correct (also accounting for the blank tiles which equal 0 points)?

So now the next step is to create a function called score_word(word) that has one parameter, word that will takes a word as the argument and returns the number of points that word is worth, yes?

Write out, step by step the logic that would achieve that. First, you’d have a variable inside the function called “point_total” (or whatever you want to call it) and set that = 0. Then, you’d have to do what? …fill in the blank here____, etc.

I find that if you write out the logic before writing any code on the computer it’s easier to visualize what you need to do. Perhaps that will help you too.

2 Likes

@lisalisaj’s advice is excellent, as the project does have some very good steps to follow.

I also think you may want to review some on dictionaries, based on you current knowledge. A good basic understanding of how to interact with them is needed for this project.

sum = sum + scrabble.keys()

you are trying to add a dict_keys object to an integer object which is not possible. If you print out the value of scrabble.keys() you’ll see something like this dict_keys(["A", "B", "C"]), a collection of every key in the dictionary. Were as you want the value of a single key in the dictionary.

1 Like

I have put my logic in the comments above before each part of the code, l am actually doing an exercise, which does not necessarily need a function

Thanks

I means l don’t know the syntax for that, but l have watched the video on dictionaries, let me watch it again


# scores

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

#input

words = input("Enter the word: ").upper()

#keep track of score

sum = 0

#Find the key from the dictionary and add the number

for letter in words:

    if letter in scrabble.keys():

        sum = sum + scrabble[letter]

print(sum)

I got the answer, thank you

1 Like

Ah, okay. I thought you were working on the Codecademy Scrabble project (on the DS and Python paths) which seems… oddly similar.