Reputation: 27
Here's my current code:
class city1:
dict = {"north":"city2", "east": 0, "south": 0, "west": "city3"}
validcmds = ['north', 'east', 'south', 'west']
input=raw_input(">>")
input=set(input.split())
validcmds=set(validcmds)
output = list(validcmds.intersection(input))
print city1.dict(output)
I don't know what's wrong with this line:
print city1.dict(output)
Basically I just want the output to be looked up in the dict in the city1 class. It's for my texted based RPG :)
Upvotes: 0
Views: 571
Reputation: 106470
First, don't use the word dict
. You mask the built-in dict()
method when you do so. Next, you can use the d.get(key)
method to retrieve elements from a dictionary.
You may want to review your code, as I'm not convinced that it works in its current state, even after you retrieve elements from the dictionary.
Upvotes: 1
Reputation: 77474
You don't use parentheses to access a dictionary. Use print city1.dict[output]
instead. However, it looks like you're making a list out of dictionary keys, and Python will complain that this isn't hashable if you try to index on a list like that.
If output
has multiple keys in it, then try this instead:
print [city1.dict[elem] for elem in output]
It's also bad practice to name a data structure after its type. I would consider re-naming dict
as something else, such as city_dict
or something more descriptive.
Upvotes: 0
Reputation: 13510
output is a list of keys. You can't key your dict with a list of keys at once. You should do:
print [city1.dict[x] for x in output]
By the way, input
is a method, and by assigning to input you deleted that command.. (not that it matters here, but you might like to know)
Upvotes: 0