Introduction

Opening your comic book store, the Sorcery Society, has been a lifelong dream come true. You quickly diversified your shop offerings to include miniatures, plush toys, collectible card games, and board games. Eventually, the store became more a games store with a selection of this week's newest comic books and a small offering of graphic novel paperbacks. Completing your transformation means offering space for local tabletop gamers. They love to play their favorite RPG, "Abruptly Goblins!" and will happily pay you per chair to secure the space to do it. Unfortunately, planning the game night has fallen to you. If you pick the wrong night, not enough people will come and the game night will be cancelled. You decide it's best that you automate the game night selector to get the most people through the door. First you need to create a list of people who will be attending the game night.

First let's create an empty list to represent the interested gamers.

In [1]:
gamers = []

Now we want to create a function that will update this list and add a new gamer to the this gamers list. Each gamer should be a dictionary with the following keys:

  • "name": a string that contains the gamer's full or presumed name. E.g., "Vicky Very"
  • "availability": a list of strings containing the names of the days of the week that the gamer is available. E.g., ["Monday", "Thursday", "Sunday"]

We'll create a function that takes two parameters: gamer and gamers_list. The function will check that the argument passed to the gamer parameter has both "name" and a "availability" as keys and if so add gamer to gamers_list.

In [2]:
def add_gamer(gamer, gamers_list):
    if 'name' in gamer and 'availability' in gamer:
        gamers_list.append(gamer)

Now let's add several gamers to the list.

In [3]:
add_gamer({'name':'Kimberly Warner','availability': ["Monday","Tuesday","Friday"]}, gamers)
add_gamer({'name':'Thomas Nelson','availability': ["Tuesday", "Thursday", "Saturday"]}, gamers)
add_gamer({'name':'Joyce Sellers','availability': ["Monday", "Wednesday", "Friday", "Saturday"]}, gamers)
add_gamer({'name':'Michelle Reyes','availability': ["Wednesday", "Thursday", "Sunday"]}, gamers)
add_gamer({'name':'Stephen Adams','availability': ["Thursday", "Saturday"]}, gamers)
add_gamer({'name': 'Joanne Lynn', 'availability': ["Monday", "Thursday"]}, gamers)
add_gamer({'name':'Latasha Bryan','availability': ["Monday", "Sunday"]}, gamers)
add_gamer({'name':'Crystal Brewer','availability': ["Thursday", "Friday", "Saturday"]}, gamers)
add_gamer({'name':'James Barnes Jr.','availability': ["Tuesday", "Wednesday", "Thursday", "Sunday"]}, gamers)
add_gamer({'name':'Michel Trujillo','availability': ["Monday", "Tuesday", "Wednesday"]}, gamers)

Finding the perfect availability

Now that we have a list of all of the people interested in game night, we want to be able to calculate which nights would have the most participation. First we'll create a dictionary which correlates each day of the week with gamer availability.

In [4]:
count_availability = {
        'Sunday':0,
        'Monday':0,
        'Tuesday':0,
        'Wednesday':0,
        'Thursday':0,
        'Friday':0,
        'Saturday':0
    }

Next we need to count the number of people every night.

We'll write a function that takes a list of gamers gamers_list and a frequency table available_frequency. The function will iterate through each gamer in gamers_list and iterate through each day in the gamer's availability. For each day in the gamer's availability, we add one to that date on the frequency table.

In [5]:
def calculate_availability(gamers_list):
    available_frequency = {
        'Sunday':0,
        'Monday':0,
        'Tuesday':0,
        'Wednesday':0,
        'Thursday':0,
        'Friday':0,
        'Saturday':0
    }
    for gamer in gamers_list:
        for day in gamer['availability']:
            available_frequency[day] += 1
    return available_frequency

Now let's use these tools to find the best night to run Abruptly Goblins!

In [6]:
count_availability = calculate_availability(gamers)
count_availability
Out[6]:
{'Sunday': 3,
 'Monday': 5,
 'Tuesday': 4,
 'Wednesday': 4,
 'Thursday': 6,
 'Friday': 3,
 'Saturday': 4}

Lastly we need a way to pick the day with the most available people to attend so that we can schedule game night on that night.

We'll write a function that takes a dictionary availability_table and returns the key with the highest number.

In [7]:
def find_best_night(availability_table):
    bestday = ''
    bestnumber = 0
    for day, number in availability_table.items():
        if number>bestnumber:
            bestday = day
            bestnumber = number
    return bestday

Now let's find the best day to host game night.

In [8]:
game_night = find_best_night(count_availability)
game_night
Out[8]:
'Thursday'

Let's make a list of all of the people who are available that night.

We'll create a function that takes gamers_list and day and returns a list of the gamers who are available on that particular day.

In [9]:
def available_on_night(gamers_list, day):
    attendees = []
    for gamer in gamers_list:
        if day in gamer['availability']:
            attendees.append(gamer)
    return attendees
attending_game_night = available_on_night(gamers, game_night)
attending_game_night
Out[9]:
[{'name': 'Thomas Nelson',
  'availability': ['Tuesday', 'Thursday', 'Saturday']},
 {'name': 'Michelle Reyes',
  'availability': ['Wednesday', 'Thursday', 'Sunday']},
 {'name': 'Stephen Adams', 'availability': ['Thursday', 'Saturday']},
 {'name': 'Joanne Lynn', 'availability': ['Monday', 'Thursday']},
 {'name': 'Crystal Brewer',
  'availability': ['Thursday', 'Friday', 'Saturday']},
 {'name': 'James Barnes Jr.',
  'availability': ['Tuesday', 'Wednesday', 'Thursday', 'Sunday']}]

Generating an E-mail for the Participants

With the best day for 'Abruptly Goblins!' determined with computer precision, we need to let the attendees know that the game night is on a night they can attend. Let's start by creating a form email to send to each of the participants that we'll fill out with data later.

In [10]:
form_email = """
Hey {name}!,
We noticed you've been dying to play the new hit game '{game}' with other enthusiastic afficionados!  Well get ready, because this {day_of_week}, the game is on! Just show up to Sorcery Society anytime in the evening to play with your fellow adventurers!
"""

Now we'll create a function that takes in gamers_who_can_attend, day, and game. It will print form_email for each gamer in gamers_who_can_attend with the appropriate day and game. Then we'll 'send' the email!

In [11]:
def send_email(gamers_who_can_attend,day,game):
    for gamer in gamers_who_can_attend:
        print(form_email.format(name=gamer['name'], game=game, day_of_week=day))
        
send_email(attending_game_night, game_night, 'Abruptly Goblins!')
Hey Thomas Nelson!,
We noticed you've been dying to play the new hit game 'Abruptly Goblins!' with other enthusiastic afficionados!  Well get ready, because this Thursday, the game is on! Just show up to Sorcery Society anytime in the evening to play with your fellow adventurers!


Hey Michelle Reyes!,
We noticed you've been dying to play the new hit game 'Abruptly Goblins!' with other enthusiastic afficionados!  Well get ready, because this Thursday, the game is on! Just show up to Sorcery Society anytime in the evening to play with your fellow adventurers!


Hey Stephen Adams!,
We noticed you've been dying to play the new hit game 'Abruptly Goblins!' with other enthusiastic afficionados!  Well get ready, because this Thursday, the game is on! Just show up to Sorcery Society anytime in the evening to play with your fellow adventurers!


Hey Joanne Lynn!,
We noticed you've been dying to play the new hit game 'Abruptly Goblins!' with other enthusiastic afficionados!  Well get ready, because this Thursday, the game is on! Just show up to Sorcery Society anytime in the evening to play with your fellow adventurers!


Hey Crystal Brewer!,
We noticed you've been dying to play the new hit game 'Abruptly Goblins!' with other enthusiastic afficionados!  Well get ready, because this Thursday, the game is on! Just show up to Sorcery Society anytime in the evening to play with your fellow adventurers!


Hey James Barnes Jr.!,
We noticed you've been dying to play the new hit game 'Abruptly Goblins!' with other enthusiastic afficionados!  Well get ready, because this Thursday, the game is on! Just show up to Sorcery Society anytime in the evening to play with your fellow adventurers!

Afterward

Wanting to include the folks who weren't able to attend the decided upon game night, let's use our currently written methods to have a second game night of the week.

We'll

  • Create a list unable_to_attend_best_night of everyone in gamers that wasn't able to attend game night on game_night.
  • Create second_night_availability by calling calculate_availability with unable_to_attend_best_night
  • Call find_best_night with the now filled-in second_night_availability, saving the results in second_night.
In [12]:
unable_to_attend_best_night = [gamer for gamer in gamers if gamer not in attending_game_night]
second_night_availability = calculate_availability(unable_to_attend_best_night)
second_night = find_best_night(second_night_availability)

Let's send out an email to everyone (whether they can attend the first night or not) whose marked themselves as available on our second game night.

In [13]:
available_second_game_night = available_on_night(unable_to_attend_best_night, second_night)
send_email(available_second_game_night, second_night, 'Abruptly Goblins!')
Hey Kimberly Warner!,
We noticed you've been dying to play the new hit game 'Abruptly Goblins!' with other enthusiastic afficionados!  Well get ready, because this Monday, the game is on! Just show up to Sorcery Society anytime in the evening to play with your fellow adventurers!


Hey Joyce Sellers!,
We noticed you've been dying to play the new hit game 'Abruptly Goblins!' with other enthusiastic afficionados!  Well get ready, because this Monday, the game is on! Just show up to Sorcery Society anytime in the evening to play with your fellow adventurers!


Hey Latasha Bryan!,
We noticed you've been dying to play the new hit game 'Abruptly Goblins!' with other enthusiastic afficionados!  Well get ready, because this Monday, the game is on! Just show up to Sorcery Society anytime in the evening to play with your fellow adventurers!


Hey Michel Trujillo!,
We noticed you've been dying to play the new hit game 'Abruptly Goblins!' with other enthusiastic afficionados!  Well get ready, because this Monday, the game is on! Just show up to Sorcery Society anytime in the evening to play with your fellow adventurers!