>>> random.choice({0:1, 2:3})
1
>>> random.choice({0:1, 2:3})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\random.py", line 274, in choice
return seq[int(self.random() * len(seq))] # raises IndexError if seq is empty
KeyError: 1
Alright pythonauts, why does this fail sometimes and succeed others? :-)
Some pydestrians might learn from the answer :)
ReplyDeleteBecause random.choice expects something that can be indexed by elements of range of its len. So range of len contains 0 and 1, 0th item is 1, but 1st item doesn't exist. Half the time it gives 1, other half it KeyErrors.
ReplyDeleteCuoc Doi La Nhung Chuyen Di
ReplyDeleteNgẫu Hứng Du Lịch
Tri Thuc Du Lich
Book Ve Du Lich Gia Re
Webother voucher code? ✓ Shop now with these 3 wilkie bit and get 40% markdown + 25% extra discount at Webother ✂ select
ReplyDelete