samedi 17 décembre 2022

Is there a way to random 5 items from dict then sum up their values?

I'm trying to randomize 5 items from a dict then sum up the value of the 5 items?

import random

mydict = {
    1: {"name":"item1","price": 16}, 
    2: {"name":"item2","price": 14},
    3: {"name":"item3","price": 16}, 
    4: {"name":"item4","price": 14}, 
    5: {"name":"item5","price": 13},
    6: {"name":"item6","price": 16},
    7: {"name":"item7","price": 11}, 
    8: {"name":"item8","price": 12}, 
    9: {"name":"item9","price": 14},
    10: {"name":"item10","price": 14},
}

randomlist = random.sample(mydict.items(),5)
print(sum(randomlist))



Aucun commentaire:

Enregistrer un commentaire