Reputation: 73
I know this is probably extremely simple and I'm forgetting something, but I cannot remember how to print the key of a dictionary. The specific problem is I have a dictionary of parts of an animal that can attack, and the value is the verb used to attack, e.g: {'teeth': 'bites'}
It looks like this:
attack_part = random.choice(list(self.attacking_parts))
print('The', self.name, self.attacking_parts[attack_part], 'you with their', self.attacking_parts.keys() + '!')
But when I use this what happens is:
The bear scratches you with their scrathes!
Upvotes: 0
Views: 100
Reputation: 48067
attack_part
variable in you code is the key of your dict
. When you are doing the self.attacking_parts[attack_part]
, you are fetching the value corresponding to attack_part
key in you self.attacking_parts
dict. Whereas doing self.attacking_parts.keys()
returns the list of all the keys present in the dict.
Hence, instead you should be using your print statement like:
print('The', self.name, self.attacking_parts[attack_part], 'you with their', attack_part + '!')
Upvotes: 3