Kim
Kim

Reputation: 33

How do you do you get a value that was randomly chosen from a dictionary

This is currently my code.

if Pokémon == 'Charmander':
    selectyourmove = input('Select your move: Flamethrower, Fire Fang, 
Scratch or Ember: ')#select your move
    if selectyourmove == 'Flamethrower':
        numberchoosing1 = random.randint(20, 22)#randomly decides 
damage of the chosen move in the range
        print(choice, 'has lost' ,numberchoosing1, 'health out of its' 
,HP, 'health!')

My dictionary is quite simple. It is:

HP = {'Char':'60', 'Squir':'50', 'Pika':'80', 'Eve':'50', 'Bulb':'70', 'Clef':'100'}

Also all these have been defined.

How do I get a value that was randomly chosen from a dictionary

Upvotes: 3

Views: 54

Answers (2)

Andrew D.
Andrew D.

Reputation: 1022

The 1st way is to use dict.popitem:

Remove and return an arbitrary (key, value) pair from the dictionary. popitem() is useful to destructively iterate over a dictionary, as often used in set algorithms. If the dictionary is empty, calling popitem() raises a KeyError.

Note, that this method randomness actually comes from implementation of hashing algorithm and python dict elements layout. That's more obscurity than randomness.

The 2nd way, the truely 'random', is using the random.choice. It doesn't modify the dict, as chooses random index in the list supplied to it:

import random
hp = {'Char':'60', 'Squir':'50', 'Pika':'80', 'Eve':'50', 'Bulb':'70', 'Clef':'100'}
print(random.choice(list(hp.keys())))

Illustration of working principle:

>>> random.choice(list(HP.keys()))
'Pika'
>>> random.choice(list(HP.keys()))
'Clef'
>>> random.choice(list(HP.keys()))
'Pika'

The list is constructed here from .keys(), but when you need pairs (like from popitem()) you could use .items():

>>> random.choice(list(HP.items()))
('Clef', '100')
>>> random.choice(list(HP.items()))
('Pika', '80')
>>> random.choice(list(HP.items()))
('Char', '60')

The same way, of course the .values() will work producing only right-hand elements of dict items though, thus won't give you much satisfaction unlike .keys() or .items() does.

PS: Then if you need the reproduce prev. run, you can fix the 'randomness' with random.seed

Upvotes: 3

Hamms
Hamms

Reputation: 5107

That depends on what you mean by "value". A dictionary is a set of key,value pairs, so technically the values of your dictionary are just the strings '50', '60', '50', '100', '70', '80', and the keys are the strings 'Eve', 'Char', 'Squir', 'Clef', 'Bulb', 'Pika'.

You can these collections by using HP.keys() and HP.values(), and you can use list() to cast these collections to lists. Then, you can use random.choice to get a random value.

So to get a random key from your dictionary (which it seems like is what you actually want), you could do:

import random
keys = HP.keys()
key_list = list(keys)
choice = random.choice(key_list)

Or, more concisely:

import random
choice = random.choice(list(HP.keys()))

Then you can get the associated value for that key with HP[choice]

Upvotes: 2

Related Questions