jeudi 9 septembre 2021

Using random.choice on a dictionary and getting error [duplicate]

While I was using random.choice() function I tried it over a dictionary. I expected that it will random keys of dictionary but instead I got some KeyError:

>>> import random
>>> print(random.choice({0:1,2:3}))
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    print(random.choice({0:1,2:3}))
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python38\lib\random.py", line 291, in choice
    return seq[i]
KeyError: 1

Can anyone explain what exactly is going on?




Aucun commentaire:

Enregistrer un commentaire