[Tutor] Deleted dictionary length not reporting

Jim Mooney cybervigilante at gmail.com
Wed Jul 17 03:48:51 CEST 2013


This is puzzling me. I have a folder with about 125 movie sound clips,
like "I'll be back," blues brothers theme, oompa-loompas, etc. I play
seven random ones on windows startup, which works fine. But just so I
don't repeat a clip, I delete it from the dictionary on each go-round.
However, to make sure that happened, I printed the key-length on each
go-round.  It should be shorter by one every time but I always get a
length of 125. (Yes, I know there are about five different ways to do
this, but I thought I'd try a dictionary ;')

Also, the printout doesn't occur until after all the sounds play, so
maybe that has something to do with it. But I also tried saving the
lengths in a list while the program ran, and got the same result. A
list of all '125'

#Using Python 2.7 on Win 7

import winsound
import random, os
random.seed()

lenlist = []
file_list = os.listdir('WAV')

for clip in range(0,7):
    file_dict = dict(enumerate(file_list))
    sound_key = random.choice(range(0,len(file_list)))
    winsound.PlaySound('WAV/' + file_dict[sound_key], winsound.SND_FILENAME)
    del file_dict[sound_key]
    print(len(file_dict.keys()))
	
''' result
125
125
125
125
125
125
125
'''


--
Jim

I sure love that Guv-a-Mint Cheese, Guv-a-Mint Cheese, Guv-a-Mint Cheese
Oh please Guv-a-Mint, Guv-a-Mint please,
Please bring back that Guv-a-Mint Cheeeeeeeeese!


More information about the Tutor mailing list