mercredi 14 février 2018

popitem() Is Picking Same Or Similar Things From a Dictionary

So I'm writing a simple code to pick a random book from the Bible, and return the name of the book and a chapter from the book. I have all the books stored in a dictionary, with randint() picking the chapter number.

from random import *

books = {'Genesis': (randint(1,50)), 'Exodus': (randint(1,40)), 'Leviticus': 
(randint(1,27)), 'Numbers': (randint(1,36)), 'Deuteronomy': (randint(1,34)), 
'Joshua': (randint(1,24)), 'Judges': (randint(1,21)) }
print(books.popitem())

The issue is in the line print(books.popitem()). It is always returning Joshua. The chapter number is usually different, but the book is always Joshua. Output:

C:\Users\*****\Desktop>randomchapter.py  
('Joshua', 12)

C:\Users\*****\Desktop>randomchapter.py  
('Joshua', 19)

C:\Users\*****\Desktop>randomchapter.py  
('Joshua', 20)

C:\Users\*****\Desktop>randomchapter.py  
('Joshua', 20)

Is there a way to fix this? Or a better way to pick at random? I tried random.choice, but was also having issues with that. I'm running Python 2.




Aucun commentaire:

Enregistrer un commentaire