I keep getting the error and can’t find where the problem lies. I’m trying so I can choose wether I want the attack the creature or both printed and what type the creature is: ‘easy’, ‘medium’ or ‘hard’, I want to store that into a variable.
JavaScript
x
22
22
1
creature = {'easy': ['chicken', 'slime', 'rat'],
2
'medium': ['wolf', 'cow', 'fox'],
3
'hard': ['baby dragon', 'demon', 'lesser demi god']
4
}
5
6
attack = {
7
'easy': ['pecks you', 'spits juice at', 'scratches'],
8
'medium': ['bites', 'charges at', 'bites'],
9
'hard': ['spits sparks of fire at', 'rends', 'smashes']
10
}
11
12
creature_easy = ['chicken', 'slime', 'rat']
13
14
cre = random.choice(creature_easy)
15
linked = dict(zip(creature[cre], attack[cre]))
16
cre_type = linked[0]
17
cre = random.choice(dict(creature))
18
19
print(linked[cre])
20
21
KeyError: 'rat'
22
Thanks in advance
Advertisement
Answer
You might want something like:
JavaScript
1
9
1
chosen_level = 'easy'
2
game_data = dict(zip(creature[chosen_level], attack[chosen_level]))
3
4
import random
5
cre = random.choice(list(game_data))
6
att = game_data[cre]
7
8
print(cre, att)
9
Output: rat scratches