Search code examples
pythonrandomdictionarykeyjython

How to select a random value in a dictionary?


I created a dictionary where each key has more than one value. The user will specify a key and the program must return one of the values ​​related to it registered in the dictionary.

For example:

Key = "The man"
Values ​​for this key = "get up" "sleep" "say hello".

When the user specify the key "The man", the program needs to return "sleep" or "get up" or "say hello".

How do I make a random values​​?


Solution

  • random.choice()

    >>> random.choice([1, 2, 3])
    2
    >>> random.choice([1, 2, 3])
    1
    >>> random.choice([1, 2, 3])
    2
    >>> random.choice([1, 2, 3])
    3