dimanche 2 octobre 2016

Random item from nested dictionary [duplicate]

This question already has an answer here:

I'm trying to choose a random item from this nested dictionary

import random
swords = {
    'Short Sword': {'attack': 3, 'defence': 1, 'price': 20},
    'Sword': {'attack': 7, 'defence': 1, 'price': 35},
    'Long Sword': {'attack': 10, 'defence': 1, 'price': 50},
    'Broad Sword': {'attack': 14, 'defence': 1, 'price': 75}
}

And if I use

randsword = random.choice(swords.items())

I get this error

TypeError: 'dict_keys' object does not support indexing 




Aucun commentaire:

Enregistrer un commentaire