lundi 24 juillet 2017

random pop from dict, serving values, not keys

            try:
                item = word_bank.pop(random.choice(list(word_bank.keys()))) 
                print(list(word_bank.keys()))
                print("The Word is: ", item)
                print("=" * 25)
                a = input("Do You Know It? (y/n):\t")
                answer = word_bank[item]

The my item variable above is returning values instead of keys from my word_bank dictionary. I printed my list of keys to try and debug the problem and no values exist, which makes me think the problem exists with the .pop method or the random.choice method. I looked into both the documentation for these and found nothing that would point out the issue.

I think it has something to do with random.choices returning an index to a key and then the pop method returning the corresponding value to that key as item.. However, when I reverse the roles and use values() in place of keys() I get a key error and cannot find a working solution to the problem.

Could anyone please shed some light on this, and help me find the most efficient way to do this?

thanks in advance




Aucun commentaire:

Enregistrer un commentaire