The Doki Doki Literature Club! Random Poetry Generator: What I Taught Complete Beginners About Programming at an Anime Convention

Crystal Panter
7Factor Software
Published in
7 min readSep 24, 2020

The Doki Doki Literature Club! is a freeware visual anime novel and video game developed by Team Salvato and released in 2017. The premise of the book is that a high school boy has just joined his school’s literature club and is getting to know four girls in the club. Periodically, you play a minigame in which you’re asked to compose short poems from a bank of provided words. Different word choices appeal to different girls, influencing the path of the story. The game is much more than a typical dating simulator, but I won’t spoil the surprises by saying any more about that. (A word of warning: This is not a game suitable for children.)

I go to a lot of fan conventions: sci-fi conventions, comic conventions, anime conventions… You name it, I’ve probably been to one at least once. Because I’ve taught at coding boot camps, I sometimes get asked to teach a panel introducing beginners to the basics of programming. I like to show people that programming isn’t as mysterious and scary as they might think by walking them through a simple example that does something fun they can relate to.

At a recent anime convention, when Doki Doki was at the height of its popularity, I used the game as inspiration for a simple Python coding demonstration in which I introduced some basic concepts of programming. And so was born the Doki Doki Random Poetry Generator.

Here’s a synopsis of the presentation I gave, along with the code so you can generate your own poems! (That’s assuming you know how to run a Python script. I’m not going to cover that in this article.)

Variables: What Words Can We Use?

In the Doki Doki minigame, the player is given a list of possible words to use in composing his poem. How will the Doki Doki Random Poetry Generator store our list of possible words? In variables!

Variables hold data. What kind of data? Well, it depends on the variable’s data type, but it can include various kinds of numbers, booleans (true/false values), and strings.

The data type we need first is a string. In Python, a string is a sequence of characters. You know what else are sequences of characters? Words!

So our Python script can store words as string variables, like this:

noun = “book”

In this example, noun is the “variable name” and book is its value. The quotation marks around the values tell Python that this is a string. If we wanted a number, we could instead write:

numero = 2

Here are two more string variables:

noun2 = “pencil”

adjective = “happy”

Two more variable names: noun2 and adjective. And their values are pencil and happy.

Lists of Words

For our poetry generator, we don’t just want one word. We want a list of possible words to choose from. In Python, we can do this with a list, a data structure that holds an ordered list of variables. It looks like this:

nouns = [noun, noun2, “foot”, “dokis”, “waifu”]

We now have a list of five possible nouns, including the two we defined in variables above, plus three more added directly in the list definition. We can do the same for lists of verbs, adjectives, and any other variables we need for our poetry generator.

So variables let us make our lists of possible words. Now, how do we do something with those words? It’s time for…

Functions: Do Something With These Words!

The Doki Doki minigame doesn’t just give the player a list of words. It asks us to do something with those words: choose some of them and arrange them into a poem. How can we choose words from our list of possible words? With functions!

Functions are sections of code that perform a specific task. Once defined, they can be reused and referenced by other functions. (That’s when they get really powerful and interesting.)

Defining a function looks like this:

def sayWords():

count = 0

words = “Book and pencil together are happy!”

print(words)

count = count + 1

Now we have a function, named sayWords, that we can run in Python, possibly calling it from another function. When the function is run, it will print out:

Book and pencil together are happy!

(I know, amazing poetry, right?)

It will also add 1 to our counter variable. (There’s no real reason for that in this function other than to show what functions can do, but it might be useful in something more complex. Stay tuned…)

So with a function, we can define a way for our random poetry generator to do something with our list of words. But how do we give it the instructions it needs to create something in the form of a poem? We’re going to need…

Algorithms: The Process of Writing a Random Poem

An algorithm is a step-by-step process by which a program can solve a problem, a problem such as composing a random poem!

For our random poetry generator, we want the program to take the list of words provided in our variables, choose some of them randomly, then put them together in the form of a poem.

Simple algorithms can be a series of actions you code into a single function, and often they will use and manipulate variables. More complicated algorithms may use many functions, many of which call other functions too.

Here’s a simple algorithm:

while (count < numero):

print(words)

count = count + 1

This algorithm first checks to see if the variable named count has a value less than the value of the variable named numero. If it does, it prints the value of the variable named words. (Remember that brilliant poem?) It then adds 1 to the value of count. This process will continue until the value of count is no longer less than the value of numero.

If the algorithm begins with count equal to 0 and numero equal to 4 — and words still equal to our brilliant poem from the previous section — the output will be:

Book and pencil together are happy!

Book and pencil together are happy!

Book and pencil together are happy!

Book and pencil together are happy!

(Repetition is an important tool of master poets!)

Poetry: Putting it All Together

So let’s look at the full code for the Doki Doki Random Poetry Generator.

Now, there’s a lot we could do to refine this code, making it much easier to maintain, adapt, and extend. I cover these techniques in the intermediate and advanced panels I lead. But for beginners, code like this shows how even very simple programming tools can do something pretty cool.

I’m not going to explain everything that’s going on in this code, but let’s look at how it uses variables, functions, and algorithms to compose a poem that will surely please at least one of the girls in the Doki Doki Literature Club.

We’re defining a function called writePoem. This function defines many variables, most of them words for our poem. And it has an algorithm for taking those words and crafting them into a poem.

When we run that function, we’ll first be asked to input many possible words from which the random poem will be generated. You can see that the function defines variables that include noun1, noun2, noun3, and noun4.

noun1 = input(“A noun:”)

The input statement means that the user will be asked to input the value for the variable. There are more efficient and elegant ways to handle all those variables, but handling them this way makes them easier for beginners to understand.

The next step is to take all those words and combine them into lists of times, adjectives, nouns, and verbs, like this:

nouns = [noun1, noun2, noun3, noun4, “MC”, “Feet”,”girls”, “dokis”,”waifu”]

You’ll notice here that we’re adding both the variables we’ve already defined, such as noun1, where the user inputs a word, and some additional words, such as “dokis”, that will always be part of the list.

Now comes the random part:

which = randint(0,3)

I’m not going to go deeply into what’s happening here, but we’re basically defining a new variable, named which, and assigning a random integer to it. That random number will be between 0 and 3.

And now the fun part:

print(“””

{} {} time

A {} {} {}

at the {} {}

“””.format(adjectives[which],times[which],adjectives[which+2],nouns[which],verbs[which],adjectives[which+1],nouns[which+2]))

OK, I know that doesn’t look very fun, but this is the part where the algorithm lays out how to combine the randomly chosen words into a poem. There’s a lot going on, much more than I’ll explain here. But it’s basically giving us a format to construct and print out a poem from the randomly chosen words.

If you run the writePoem function in Python and input the words requested, it will randomly select words from the provided lists and print them out as a poem. Let’s try it!

Will the girls in the Doki Doki Literature Club like it? No guarantees, but it’s worth a try! And we did it all with variables, functions, and algorithms.

--

--