0

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 :)

3 Answers 3

1

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.

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

Comments

0

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)

Comments

0

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.

1 Comment

Thank you :) I assumed as it was part of the city1 class that calling it dict would be ok :) just looked through my code and realised I was being very stupid :) thank you :) I often get into a muddle then work it out myself, I should really have seen this one!!

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.