I just made a Secret Santa Picker with Python! Isn’t that a fun thing to say…
I am still trying to become a more useful human, I keep hearing everywhere that people need to know how to code. I am attempting to learn Python by doing useful things. I cannot learn by just reading a book and absorbing information, I have to have a goal to accomplish. I need a project to pursue.
Today one of my family members mentioned our annual Secret Santa game. We usually pick out of a hat, but now we are spread out all over the country so picking out of a hat is not going to be easy. I figured this would be a good challenge to put my python skills to the test. My new project was to make a Secret Santa picker. I knew I was going to use Python’s random library. After playing with random.shuffle, I tried random.sample. This seemed to work much better for what I wanted to do.
If you don’t wanna read this whole thing you can just jump straight to the code and start using it – – https://github.com/germfil/SecretSanta
I started out with a list of names in a text file and created a python list from that text file.
After that I got a count of the names in the list and used that count to set up my randomization.
Now that I had my original list of names (names) and my new list of names in random order (newnames) I just needed to figure out how to print them or write them to a file quickly and easily. This took some trial and error but I finally came up with a good idea.. I knew you could reference items in an list by specifying the list[itemposition]. I set up a counter variable that I could use to match the positions of the list items. I did this using the len() function. The len() function told me how many items I had in my list, and it started counting at number 1. The list items start at position 0, so in order to get the proper value for my counter, I had to start off by subtracting 1 from it.
Now if I have 10 names in my list, my counter variable will start at 9, not 10…. This is because the items in my list would be numbered 0-9, not 1-10. It is still 10 items in the list, but there is no item in position 10, so if I were to reference it I would get an error. Now I could build my basic loop:
for name in names: print(name.strip('\n') + " Shops for "+ newnames[counter]) counter=counter-1
This just loops through all the names in my original list(names) and for each one it prints out “Name1 Shops for NewName[counter]” and then as part of the loop it decreases the number of the counter. So if i had 10 names in my list each iteration of the loop would do this:
Name1 Shops for NewName
Name2 shops for NewName
Name3 shops for NewName
and so on..
Now this does not just reverse the names.. NewName could be any name in the list. The random.sample() function randomized all the names in the NewNames list, so it is totally possible that NewName is actually the same as Name1… For this we had to add a bit of logic to catch it if it was the same. People cannot be their own Sectret Santa..
for name in names: if name==newnames[counter]: print "Somone got themselves... start over...." break print(name.strip('\n') + " Shops for "+ newnames[counter]) counter=counter-1
I added an if and a break. The if just says, if the names are the same, then print out a message and break the loop. It basically just stops my script, and then I need to run it again. This is basically all I needed to get it working. I spent a couple more hours tweaking it and trying to get it to do exactly what I wanted it to do. I wanted it to retry itself and not finish until it had a good run. I also wanted it to write the output to another text file instead of to the console. So I added a couple more things and changed the output to a file. You can check out the whole thing here – https://github.com/germfil/SecretSanta
Feel free to use it and make it better…. I have no idea what I am doing.. 🙂 Have fun playing Secret Santa!