Programming with dogs!

For people who have never written any code, it can be easy to think that computer programming is some arcane and strange process where you type 1s and 0s into a computer and somehow Persona 4 comes out on the other side. On some level that is what’s happening, but the reality is that most day-to-day coding actually isn’t any more difficult than a gradeschool-level algebra class. If you’ve ever had to “solve for x” on a worksheet, you probably have the know-how to understand how programming works.

In most “normal” programming languages, you have things called “variables.” These variables are used to store information, such as a number or a line of text. After a variable is given this information, we can then put that information through an “algorithm,” which will tell the computer what to do with that information.

Let’s start with an example, and then walk through what’s happening in this code.

Code:

greeting = "Hi good boy! You're such a good dog!"
print(greeting)

Expected Result:

Hi good boy! You're such a good dog!

So let’s break down what’s occurring here. In the first line of this code, we tell the computer, “I want you to remember that when I say the word ‘greeting’, you should associate that with this line of text.” From now on, that line of text is stored in the variable called “greeting”.

In the second line of this code, we reference the variable called “greeting”, and tell the computer to print the contents of “greeting”. Print here just means that the program will send back that text as output.

This code is okay, but it doesn’t really feel like it’s “doing” anything yet. Let’s now look at some cooler things we can have this program do.

Code:

numberOfHandsomeStudDogs = 6
numberOfFetchingLadyDogs = 5
numberOfSexyDogs = numberOfHandsomeStudDogs + numberOfFetchingLadyDogs
print("The number of sexy dogs at the dog park today is...")
print(numberOfSexyDogs)

Expected Result:

The number of sexy dogs at the dog park today is...
11

Now we’re getting somewhere! In this program, we define three variables.

In the first line, we declare that there are 6 handsome stud dogs.

In the second line, we declare that there are 5 fetching lady dogs.

In the third line, we declare that the total number of sexy dogs at the park should be the sum of how many male dogs there are and how many female dogs there are.

In the fourth and fifth lines, we print our results.

On a fundamental level, this is pretty much how computer programming works. We give the computer some information (number of male and female dogs), tell the computer what operations we want it to do on that information (add the two numbers together), and then receive some kind of output (print the result).

So now our program is doing something, but it still feels pretty basic. Like, so basic that it doesn’t feel like there’s much of a point to having the computer do it instead of having a human do it.

To go forward from here, we should next talk about Functions.

In the examples that we’ve already covered, the computer knew to read the code starting at line 1 and go down line by line until it reached the end of the code. But if we wanted to do a more repetitive task, it would get really annoying to paste in the same lines of code over and over again. Plus if we needed to fix something about that repetitive task, we would have to change it in so many places!

With functions, we can tell the computer to remember a set of lines of code, and then do them over and over again whenever that function is called. It’s similar to defining a variable, but instead of storing information, we’re storing instructions.

Let’s say as part of a text adventure game, we need to be able to report how many dogs are currently in the game’s dog park. To make this process easier to do over and over again, we could write a function that formats the report how we want it, so that we can call the function each time we want to report the dog park population.

(At this point, we are going to start glossing over some things. For example, “def” is shorthand for “define this function for me please”, and “%i” is a shorthand way of formatting numbers into a line of text. The specifics here don’t matter for now: the point is just to understand at a basic level, at a glance, what’s going on when we look at these lines of code.)

Code:

def ReportParkPopulation(m, f):
    print("Population Report:")
    print("There are %i sexy male dogs in the park right now!" % m)
    print("There are %i sexy female dogs in the park right now!" % f)
    total = m + f
    print("There are %i sexy dogs in all!" % total)
    print()

numberOfHandsomeStudDogs = 3
numberOfFetchingLadyDogs = 7
ReportParkPopulation(numberOfHandsomeStudDogs, numberOfFetchingLadyDogs)

numberOfHandsomeStudDogs = 9
numberOfFetchingLadyDogs = 12
ReportParkPopulation(numberOfHandsomeStudDogs, numberOfFetchingLadyDogs)

numberOfHandsomeStudDogs = 129
numberOfFetchingLadyDogs = 195
ReportParkPopulation(numberOfHandsomeStudDogs, numberOfFetchingLadyDogs)

Expected Result:

Population Report:
There are 3 sexy male dogs in the park right now!
There are 7 sexy female dogs in the park right now!
There are 10 sexy dogs in all!

Population Report:
There are 9 sexy male dogs in the park right now!
There are 12 sexy female dogs in the park right now!
There are 21 sexy dogs in all!

Population Report:
There are 129 sexy male dogs in the park right now!
There are 195 sexy female dogs in the park right now!
There are 324 sexy dogs in all!

The computer is still starting by reading all of this from top to bottom, but now when we read it, we have to “jump around” a bit to make sense of it.

First, we define a function to print a report of the dog park’s population. The function expects to receive two variables as input, which it will call m and f once it has them. Using these two variables, it will print a report in the same format each time, changing only the numbers. It doesn’t actually do this quite yet though; it’s only being told, “This is what you will do when we call you.”

Once we’re done defining the function, we define how many hot male dogs there are (3) and how many hot female dogs there are (7).

After that, we call the function we defined earlier, and provide it with the variables we just defined so that it knows what to use when it prints the report. As soon as ReportParkPopulation is called, we “follow” the code by going up to where we defined it earlier, read that section from top to bottom, and then go back to where we left off in the main body of the code.

Back in the main body, we now redefine how many unbelievably attractive male and female dogs there are, and print a new report. The report gives us the same thing but with the new numbers. Then we redefine the number of amazingly breedable male and female dogs one more time, and print a report with those new numbers.

Now our program feels like it has a bit of a purpose! We actually have a task that we want to accomplish (print a report on how many dogs are in the park in this text adventure game), and we are using computer programming to solve it in an effective way (using functions to handle repetitive tasks).

To expand on this text adventure game, let’s say that there’s a dog breeding portion of the game. We want the game to remember the traits of all of our dogs (type of hair, shape of ears, male or female), and then when we breed two of the dogs, we want the game to randomly select traits from each parent and give them to the puppy.

This seems like it’s going to be a lot to keep track of, and could get messy. So to keep all of this information organized, let’s talk about Classes.

On a basic level, classes let us define a sort of “structure” or “template” to keep a bunch of different information together under the same variable name. So as an example, let’s write the dog breeding function, as well as a class called “Dog” to keep everything organized.

Again, don’t worry if you don’t follow every line of this code. Some of it is jumping ahead beyond what we’ve covered, but we’ll go over the key points here. The purpose isn’t to know what every dot and parenthesis means, but just to see the overall idea of what’s going on.

Code:

from random import choice as Choose

class Dog:
    def **init**(self, hair, ears, sex):
        self.hair = hair
        self.ears = ears
        self.sex = sex
    def Describe(self):
        print("This dog has %s, %s, and is %s." % (self.hair, self.ears, self.sex))
    def GetJiggyWithIt(dog1, dog2):
        if dog1.sex == dog2.sex:
            raise Exception("Unviable pairing.")
        hair = Choose(\[dog1.hair, dog2.hair\])
        ears = Choose(\[dog1.ears, dog2.ears\])
        sex = Choose(\["male", "female"\])
        return Dog(hair, ears, sex)

Fido = Dog("short brown hair", "tall pointy ears", "male")
Lucy = Dog("long black hair", "long floppy ears", "female")
Spot = Dog("short white hair with black spots", "long floppy ears", "male")
Socks = Dog("short black hair with white paws", "tall pointy ears", "female")

print("Meet Fido!")
Fido.Describe()
print()

print("Meet Lucy as well!")
Lucy.Describe()
print()

print("Lucy and Fido have a puppy named Max:")
Max = GetJiggyWithIt(Fido, Lucy)
Max.Describe()
print()

print("Now meet Spot!")
Spot.Describe()
print()

print("And his mate, Socks!")
Socks.Describe()
print()

print("Spot and Socks have a puppy named Lucky:")
Lucky = GetJiggyWithIt(Fido, Lucy)
Lucky.Describe()
print()

print("A few years pass...")

print("Now that they're grown up and both are in the mood, Max and Lucky start sniffing each other, and decide to have a puppy together.")

try:
    Alex = GetJiggyWithIt(Max, Lucky)
    print("Meet Max and Lucky's puppy, Alex!")
    Alex.Describe()
except:
    print("However, Max and Lucky are the same gender, so humping each other did not accomplish very much. But they had fun trying.")

Okay, so there’s a lot more going on in this one, but for now let’s just look at a couple of things.

When defining the class “Dog”, we defined it so that when creating a variable of this class, it should expect to be told a description of the dog’s hair, ears, and sex. We also gave Dog a function to report all of this information in the form of a brief sentence.

When defining the function “GetJiggyWithIt” for mating, we define it so that it expects to receive two “Dog”s, and then will return a third dog that has attributes randomly chosen from either parent.

Now that we have all of this set up, we define four dogs (Fido, Lucy, Spot, and Socks), and then we start having them breed and describing what all of them look like.

As part of the mating function, we also tell it not to have the dogs make a puppy if the two parents are the same sex. This is why we say “try:” and “except:” when attempting to breed Max and Lucky: when writing the code, we don’t actually know yet whether they will turn out to be the same sex or opposite sexes, so we’re telling the code, “Try to breed them, but if the breeding function says that that isn’t quite going to work out, we’ll cancel that and do this other thing instead.” So depending on whether some of the dogs turn out male or female, we could have a noticeable difference in our expected results.

Expected Results:

Meet Fido!
This dog has short brown hair, tall pointy ears, and is male.

Meet Lucy as well!
This dog has long black hair, long floppy ears, and is female.

Lucy and Fido have a puppy named Max:
This dog has long black hair, tall pointy ears, and is female.

Now meet Spot!
This dog has short white hair with black spots, long floppy ears, and is male.

And his mate, Socks!
This dog has short black hair with white paws, tall pointy ears, and is female.

Spot and Socks have a puppy named Lucky:
This dog has short brown hair, long floppy ears, and is male.

A few years pass...
Now that they're grown up and both are in the mood, Max and Lucky start sniffing each other, and decide to have a puppy together.
Meet Max and Lucky's puppy, Alex!
This dog has short brown hair, tall pointy ears, and is female.

Alternative Expected Results:

Meet Fido!
This dog has short brown hair, tall pointy ears, and is male.

Meet Lucy as well!
This dog has long black hair, long floppy ears, and is female.

Lucy and Fido have a puppy named Max:
This dog has long black hair, long floppy ears, and is male.

Now meet Spot!
This dog has short white hair with black spots, long floppy ears, and is male.

And his mate, Socks!
This dog has short black hair with white paws, tall pointy ears, and is female.

Spot and Socks have a puppy named Lucky:
This dog has long black hair, tall pointy ears, and is male.

A few years pass...
Now that they're grown up and both are in the mood, Max and Lucky start sniffing each other, and decide to have a puppy together.
However, Max and Lucky are the same gender, so humping each other did not accomplish very much. But they had fun trying.

All in all, this piece has covered a lot of the fundamentals of computer programming. There are entire curriculums of stuff you can learn beyond this, but if you were able to follow along with most of it, then you have a real foot in the door for understanding all of this. Thanks for reading! It’s the hope that breaking these things down makes the concept of programming feel more accessible, like something you can learn whether you think dogs are sexy af or whether you think they’re only beautiful platonically.

Article written by an anonymous author (November 2022)

Questions, comments or concerns? Check out the discussion thread on ZooCommunity, or join our Discord server!

https://zoocommunity.org/thread-1632.html

Join the Discord

Related posts

Interview with Eggshell

Tarro: Hello and welcome to this interview! Today dear readers you are in for a treat. We’re joined by the…

How to deal with curious people

As the zoo movement grows larger and larger, there’s going to be more eyes on our community. And while there…