# giver = ['Colin','Brynn','Jill','Ethan','Grammy','Grampy'] giverdict = {'Colin':'cgranger@pvlearners.net','Brynn':'girlygirlz0916@gmail.com','Jill':'jgranger@landadvisors.com','Ethan':'egranger@landadvisors.com','Grammy':'jeannemgranger@gmail','Grampy':'dwgranger@comcast.net'} receiver = ['Colin','Brynn','Jill','Ethan','Grammy','Grampy'] from random import choice import sys def matchSecretSantas(giverDict,getterList): santasDict = {} giverLength = len(giverDict) getterLength = len(getterList) if giverLength <> getterLength: print "Length of lists don't match. stopping." sys.exit() for giver in giverDict: isValidMatch = 0 while isValidMatch ==0: giverEmail = giverDict[giver] theGetter = choice(getterList) if giver <> theGetter: santasDict[giver] = {'email':giverEmail,'getter':theGetter} getterList.remove(theGetter) isValidMatch = 1 # if the last element matches theGetter, then break. Really needs to clear the dict, reset the getterList and start over. # do i need to make a copy of the getterList to reset it? elif giver == "Grampy" and theGetter == "Grampy": santasDict.clear() break # sys.exit("Grampy = Grampy. Please rerun") return santasDict try: dict = matchSecretSantas(giverdict,receiver) except: print "Didn't work" print dict print "Dict has " + str(len(dict)) + " elements."
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question