NukeX Giveaway -- How I chose the winner with Python.

  Comp Newsletter For Patreon Supporters Python


Congratulations to Andriy Koval for winning the NukeX license giveaway! As a bonus for being a Patreon supporter, I wanted to show you the thought process & Python script I wrote to help me choose a winner.

To start, I was able to download a CSV report for newsletter subscribers who entered the giveaway, as well as another CSV report for current Patreon supporters. I then needed to find an easy way to get all the email addresses from both CSV's into a Python-friendly list. I'd never had to use Python to navigate a CSV file before, so it was a fun learning experience!

As you know, subscribers of Ben's Comp Newsletter could enter this draw simply by clicking the sign-up link in Issue 064, and since you're a Patreon Supporter, you got two entries! However, there was no guarantee that all Patreon supporters would click the link in the newsletter for that second entry, so I also wanted to code up a solution to account for that.

Lastly, this draw had to be completely random. Rather than closing my eyes, scrolling through a list, and putting my finger on the screen to make my selection, I wanted the computer to choose for me to remove any human bias.

Check out the final code below, commented for clarity.

 1# Import the modules we need.
 2import csv
 3import random
 4
 5# Create lists to hold email addresses.
 6newsletter = []
 7patreon = []
 8
 9# Define filepaths for the CSV's on disk.
10newsletter_csv_file = open("C:UsersbenmcDesktopnewsletter.csv", "r")
11patreon_csv_file = open("C:UsersbenmcDesktoppatreon.csv", "r")
12
13# Open the newsletter CSV, and add all the addresses from the "email address" column (column A).
14for emails in csv.reader(newsletter_csv_file, delimiter=','):
15    newsletter.append(emails[0])
16# The first item in the row is the header, "email address", so we can remove it.
17newsletter.pop(0)
18
19# Same deal for the Patreon members CSV, except the email addresses are in column B in this instance.
20for emails in csv.reader(patreon_csv_file, delimiter=','):
21    patreon.append(emails[1])
22patreon.pop(0)
23
24# List to hold all email addresses.
25email_list = []
26
27# Remove Patreon subscribers from newsletter list.
28print(str(len(set(patreon).intersection(newsletter)))+" Patreons were duplicates, and have been removed.")
29for email in set(patreon).intersection(newsletter):
30    newsletter.remove(email)
31
32# Add Newsletter subscribers to the email list.
33email_list.extend(newsletter)
34
35# Add patreon subscribers into the same list, twice.
36email_list.extend(patreon)
37email_list.extend(patreon)
38
39# Randomly pick a winner.
40print("nThe winner of the NukeX license is: "+str(random.choice(email_list))+"!")
41
42"""
43-----  RESULT  -----
44
4524 Patreons were duplicates, and have been removed.
46
47The winner of the NukeX license is: <email address hidden for privacy>
48"""