Computational Thinking for Mortals

Computational Thinking for Mortals

Many people believe they are not "technical" and therefore, they have no capacity to understand technical ideas.

This is pretty meaningless in the 21st century. If you were not technical, you'd not use tools. Anyone who can use tools - like forks, cars, and whatever device you are reading this article on - is technical.

So, unless you're reading this on a engraved rock, while eating noodles with your fingers, let's dispense with the idea about not being technical. Besides, if it was recommended you read this before you show up at ZipCode, well, you'd better get comfortable with being technical.

Technology is merely tools made by humans for humans. While learning to program can be a difficult task, we've found at ZipCode that our students sometimes struggle with a few key fundamentals when mixed up with the other new stuff they are handling. It'd be good if you could spend some time reading this and understanding some fundamentals concepts before you arrive at ZipCode and work starts.

I am certain you can understand all the stuff in this article. The more times you are presented with key ideas, the easier it becomes for you to learn them.

Coding and Programming

Coding and Programming are pretty much the same thing. At least, it is for people who are just starting out like perhaps you are. So we'll use either term when talking about activity around the creation of software, or programs. This will help you to learn to think in ways that make the creation of software easier.

This mindset is called Computational Thinking, and CT, as it is known, is a key skill to have in today's world. CT is as fundamental as reading, writing, and arithmetic; it should be a part of your analytical toolset. CT is also different than programming. CT can be done without a computer, and the schools in New Zealand do just that.

New Zealand schools teach the concepts of CT to school children of all ages, laying the intellectual groundwork for thinking in computational ways long before the students have access to a programming language to create programs. CT is really that fundamental.

CT refers to the thought processes involved in expressing solutions as computational steps or programs that could be carried out by a computer. As we will discuss in the ZipCode course, the technically precise terms for parts of these steps, for the programs we write, are algorithms.

At the core of CT are a few crucial insights:

Computers are very literal things. If you are not precise, correct and very detail-oriented, you will have severe troubles in programming computers.

Coding is easy, figuring out WHAT to code, is hard. Using CT and thinking about and carefully analyzing problems oftens gets lost when you're a computing beginner. This is bad on many levels.

Decomposing problems is essential to programming. The Scots have a saying "many a mickle makes a muckle", where mickle implies a small thing and a muckle a much larger group of things. Most computing problems, and their resulting solutions, are large muckles that need to be reduced to a large number of mickles. You have break large problems down into a bunch of smaller problems, then collect and compose all of the smaller solutions back up into the larger solution.

Computer languages are meant to make programming easier for humans. These are, as I am sure you know, a specialized vocabulary seeking to be very precise in allowing humans express how they get a computer to perform complicated and repetitious tasks.

But learning to think computationally is crucial to success as a programmer. You can think computationally about many things, even something as mundane as washing up your dishes. Perhaps you were taught, do the glassware first, then the plates and the utensils, and finally your pans. You might do it this way hundreds of times and never think about it, but you are doing things, CT-wise in any case, by following well-thought out steps.

To understand how to do these things, well, let's wander down into some details.

Programming Ideas

In thinking about programming, there are some topics that everyone should be exposed to in a clear fashion, not in a covered in a thick, steaming technical sauce. So, let's talk about coding from a very high level. And to do that, I want you to consider the humble thermostat.

What does your home's thermostat do? Now, it's possible you're reading this in one of those charmed locales in the world where you have no thermostat in your home, but much of the industrialized world does have such things. A thermostat is a simple device, a computer of sorts. Describing _what_it does and why it does it is more important to us right now than How it does what it does.

A thermostat is essentially an active thermometer: it measures the current ambient temperature, and compares that to a setpoint, and if the measured temp is higher than the setpoint, it turns on the air conditioning to make the room cooler and bring the temperature closer to the setpoint. If the room's ambient temperature is lower or cooler than the setpoint, it will turn on the heater to raise the room's temperature heating the room closer to the setpoint.

This is what is commonly called a feedback loop - the device operates continuously, keeping you warm, or cool, without you worrying about it. And that's the critical WHY. We build these tools so they can handle mundane tasks that free us up to waste time in front of Netflix. A thermostat is a computer, of sorts, a very simple one, and one that can cleverly do what we need it to do without any software at all. That doesn't stop engineers from building more complicated (and perhaps more brittle) versions, ones with software inside, that are more expensive than simpler ones. So there is that.

Going further, we'll break this behavior down into component pieces, and consider each in turn.

The notion of a program

You most likely know what a recipe for a chocolate cake is - a step by step procedure that you can use to create a tasty treat. (Given you have a source for the raw ingredients.) And you can probably imagine that making an apple pie is somewhat similar to making a cake, but different enough that you would not follow the instructions for a cake to make a pie.

Yum! Apple Pie
Photo by Dilyara Garifullina / Unsplash

In a very real sense, these procedures for cooking a cake or a pie are very much like a computer program. Many also refer to these recipes as algorithms, which is just a $5 word for recipe. Think of software as the instructions to have a computer follow some very distinct and precise steps to accomplish some task. These tasks are broken in very precise steps, and each step clear and precise enough for a computer to perform. Being vague with a computer is usually an exercise in frustration.

Let's use the example of multiplication. If I want to multiply the number 5 times 6, I can specify a very simple program to do so. I can create 5 different 6s and add them together. I just have to make sure I count out my 6s correctly. (Yes, this is silly way of doing it, but... it works. Every time.)

result = 6 + 6 + 6 + 6 + 6

/* printing the value of result would be 30 */

Now, something quick: see the text between the slash-stars? /* this kind of thing */ That's what's called a comment. It's for you. It's a little description or explanation of something that is happening in the program. The computer tends to ignore whatever is in comments, and some languages have different ways of showing comments. But all computer languages have comments and learning to spot them helps you to understand code better.

# this is a comment in Python.
// this is a comment in Swift or Java.
/* this is a comment in Java, C and C++. */
/* yes, Java can do several styles of comments. */

I'll use comments to explain things in the programs below. And now, back to your regularly scheduled explanation.

Nowadays, we can interact with computer programs in lots of different ways. Like your voice: "Siri, where is the nearest taco place?" and deep inside of Apple a computer program performs a list of tasks

  • records and analyzes the sounds of what we said
  • matches the sounds to a list of words
  • analyzes the sequence of words to decide on meaning and depending on that,
  • asks a different program to find a taco restaurant near us
  • which provides a map, a picture and other data to help us get tacos!

(more or less)

So very often you will see the notion of a program as something that takes input data, performs some task on the data, and produces some output data. And really, that's a fine way to conceptualize computing, and the notion of a program.

Why in Coding

A key focus of this article is to keep explaining the why of it all. Many times students of programming get lost in the how and what often forgetting the why. So we will start with why as we discuss these key ideas.

Data Types

For a computer to keep track of things, it reduces EVERYTHING to a long sequences of 1s and 0s. They are, after all, merely Binary Computers, and binary computers only know how to deal with 1s and 0s. But humans are terrible at handling 1s and 0s.
For example, you might take a very long time indeed to figure out what this pattern means.

01010111 01100101 00100000 01110100 01101000 01100101 
00100000 01010000 01100101 01101111 01110000 01101100 
01100101 00100000 01101111 01100110 00100000 01110100 
01101000 01100101 00100000 01010101 01101110 01101001 
01110100 01100101 01100100 00100000 01010011 01110100 
01100001 01110100 01100101 01110011 00101100 00100000 
01101001 01101110 00100000 01001111

We don't know, from looking at the 1/0s above what type of data that pattern is. It might be an image, a number, a spreadsheet, a word doc, the trailer to a movie, or it might be random data. We cannot tell because we do not know what type of data it is.

Since the very beginning of electronic computing, binary has been the best means for data to be handled by a machine, and an awful means for a human to deal with data. Which is why we had to create other ways of representing binary ones and zeros in higher level forms for humans to understand.

In this article we will look at these types of data:

  • string for text data
  • boolean for simple true/false (or yes/no) data
  • numbers with two different types
    • integer for whole numbers
    • float or "floating point" numbers with decimal points

Data types are agreed upon ways to model important real world ideas within a programming language, and a key bridge of understanding between humans and their computer programs.


Think about the words on this page. The text here is made up of a bunch of letters, and spaces. Now, when we write by hand, we don't really think about the space between the words, do we? If we truly ignored the notion of space between the words, wewouldendupwithtextlikethis. And while possible to read, our modern eyes are trained on well-edited texts, tires us pretty quickly.

So yes, what we see as text in this article is really a series of letters and spaces strung together in a sequence like a string of beads - line after line, paragraph after paragraph.
In modern computing, that kind of data is often called a String.
It is one of the most fundamental aspects of coding, the manipulation of strings by programs to transform, present or store text in some fashion.

Many programming languages use some kind of quote or double quote to show where strings start and end.
So a string like "the quick brown fox" would be a string from the 't' to the 'x'. And notice the three spaces within the string.
If they were not there, the string would be "thequickbrownfox".
And that's important, because to the computer, if it keeps these two strings around, it doesn't really understand that 'the' and 'quick' are just two common english words, the spaces are there to retain more of what the human meant.

No, to the computer, each letter, including the space 'letter', is just a piece of data.

And it's your first data type. a string - a string of characters (letters and spaces and punctuation) kept altogether for some use.
Here are some strings for you to consider.

"the quick brown fox"
"The New York Times"
"And lo, like wave was he..."
"Hello, World!"
"On sale for $123.99!!"
"Pi is approximately 3.14159"
"Merge left at the ramp to the right, the restaurant is on the right"
"He said, "Wait there is more!""

And look at the last one. There are double quotes INSIDE the string. Let's just ignore that for a second. It's still just a string.


There is a very important kind of data in computing that you see pretty much everywhere - boolean.
The idea of a boolean value is that it is one of two alternatives: true or false; "the current temperature is less than 72.0 degrees" is an expression that when you consider it, has either a true or false result. Several of the things you'll see below are related to figuring out whether an expression you've written in code is true or false and behaving differently in each case.

boolean is named after an English mathematician, George Boole.

Time for another snippet of code.

result = 4 > 5

/* the result should be false */
/* because 4 is NOT greater than 5 */

home-team-wins = home-team-final-score > visitor-team-final-score

/* result home-team-wins depends on what */
/* the two final scores ended up being. */

But in both cases, result and home-team-wins are either true or false, depending on the expression to the right of the equals sign.

Numbers (Wholes and Reals)

Now, I would expect all of you to have some idea that in the beginning of the Age of Computers, they were used primarily for numerical processing. Things like large tabulations of census reporting, profits and losses of businesses, scientific computation and military ballistics.
Yes, for a long time, computers were used to compute a bunch of very precise tables that could be used to predict just how far an artillery shell would go given the initial velocity, angle of launch & initial height above the ground.

To do any of these tasks, computers had to input, store, manipulate, compute and output numerical data. These numbers were often one of two different types: whole numbers and real numbers. Computationally these are called Integer and Floating Point (generally referred to as floats) numbers.

Integers: 5, 7, 0, -3
Floating Points: 0.5, 1.2, -34.5, 12345.678, 0.00000028

These two kinds of numbers are generally used in different ways for different things, but let's just say, there are two different kinds of numbers. And it does matter. For now.


When you're building programs, you have to track data in many ways that often changes. Think about all the apps you have on your smartphone. Each one probably tracks your name, your email address, and other information about you.

Many cars these days have computers in them. Programs running on the computer in your car might track the state of various things. Is it running? (boolean), How fast is it going? (float), How much fuel is in the fuel tank? (float), What diagnostic codes are currently active within the car? (string(s)).

Inside a computer program these values are tracked using variables. Variables are just like the things you studied in algebra (you did study algebra, didn't you? way back? I bet you remember it better than I do).

Variables track the way data changes within a program while it runs. A program to land a spacecraft on Mars would track speed, direction, altitude, 'isEngineBurning', outside temperature, 'haveWeLanded' and other factors to complete its flight safely.

Now, you might be thinking, well those are all numeric and boolean, and you'd be right. So what's the deal with Strings being so important? Well, on websites, in databases, in word processing documents, note taking apps, social media apps, and others, the predominate data type is strings. Names, emails, phone numbers, messages, documents, and lots of other data is primarily strings (or text). After a course like ZipCode, many of your first projects will have large amounts of string data being handled.

Examples of variables:

var isLanded : boolean = false
// the spacecraft is still landing

var currentSpeed : float = 55.0 
// the car is moving at the speed limit

var currentCompassDirection : integer = 181 
// the ship is heading south-ish

var userFirstName : string = "Sally"

var summerEssay : string = "What I did this Summer. I was very bored this summer during COVID. I was never allowed outside or to camp. The End."

These are all examples of variables, values that are able to change over the course of the running of a program. The term var in this case just an indication to the computer that we are declaring a variable. (Telling it that we want the computer to keep track of it.) The program might keep changing them (like the currentSpeed) as conditions change. And that's the crucial point, a variable allows you change the value any time you'd like.

var currentSpeed = 55.0
/* we tap the brakes */
currentSpeed = 45.0
/* we stop at red light */
currentSpeed = 0.0
/* we turn onto the street we live on. */
currentSpeed = 30.0

In each of these steps we are setting the variable to a different value. We could also do something like:

var net-profit = 0.00
var taxes = 0.00
var total-income = 12000.00
var total-expenses = 6000.00
var tax-rate = 0.015

net-profit = total-income minus total-expenses
taxes = net-profit times tax-rate

print(net-profit, taxes) /* it should print 6000.00, 90.00 */

In this program we not only set the variable net-profit, but we also performed a get of the variable to use in another compution to determine taxes.

When programing, variables are used for temporary storage, for indexing, for status information and all sorts of other uses. We generally name variables something meaningful to our program. Here is a bad example of how NOT to name variables.

var foo = 0.00
var vanilla = 0.00
var airplane = 12000.00
var grass = 6000.00
var gorilla = 0.015

foo = airplane minus grass
vanilla = foo times gorilla

print(foo, vanilla) /* it will still print 6000.00, 90.00 */

By renaming all the variables to meaningless things, we've lost the 'human' point of the program. BUT we would end up with the exact same result, because the computer doesn't care what we call the variables. (Weird, huh?) So take care when naming variables, it's important to your fellow humans when they read your code.

The Flow of a Program

In almost all cases, a computer program starts at the top of the page of code and proceeds down Step by step, line by line.
This idea is the control flow or flow of control of a program made up of computer code.
Many of the things we will study allow us to manage the control flow of a program.
This program adds up the total number of students in a school.
It has a very simple control flow.

first-graders = 13
second-graders = 17
third-graders = 14

total-student-count = first-graders + second-graders + third-graders

Notice how this program is so trivial, you'd probably just do it in your head.

add 13 + 17 + 14 for 44 total students.

The key point here is the sequential top-to-bottom nature of a program.

The idea of a Boolean Expression

As I mentioned before, an expression is a very important notion in programming. Expressions seek to encapsulate some condition, and try to determine a result from the condition. Oftentimes, the expression is evaluated to be a boolean result, a true or a false.

home-team-final-score = 21
visitor-team-final-score = 13

home-team-wins = home-team-final-score > visitor-team-final-score

/* result home-team-wins is true! */

The result here is that the home-team-wins is evaluated to true because home-team-final-score (21) is greater than visitor-team-final-score (13). (yay!)

Other expressions that evaluate to true or false might be:

  • temperature is less than 55.0 degrees
  • is current direction North
  • was the flight late
  • is the front door closed
  • is the guy older than the gal
  • isPlayerOneAlive?

But notice these expressions do NOT result in true or false.

  • the current score for the home team (21)
  • the dog's name ("Fido")
  • how tall you are (1.68 meters)
  • add 16 to 12 and divide by 2 and then add 2 (16)

These are expressions too, they're just not boolean expressions. They result in something other than a true or false. (They all result in either a number or a String when they are evaluated)

One can also create complex boolean expressions using AND, OR and NOT:

iAmMiserable = ((wet AND cold) OR (cold AND hungry))
/* iAmMiserable is a boolean variable here */

Notice how this expression requires that the state of 3 different variables needs to be determined before we can decide if we are miserable or not. What is the result if I am wet AND hungry? How about if I am wet AND cold AND hungry? How about if I am not wet OR cold OR hungry? And what about if I am cold AND wet?

Notice also a common sight in computing languages of how parenthesis are used to group how an expression should be evaluated. Each of these three google searches might bring back three different answers

/* search chevy cars in delaware? */
search1 = (car OR chevy) AND delaware
search2 = car OR (chevy AND delaware)
search3 = car OR chevy AND delaware

In the next two items, IF statements and Loops, you will see why the boolean expression is so important. It lets you design code that can decide the state or status of something and then potentially do something about it.

fuel-tank-level = 0.1

turn-on-refuel-light = fuel-tank-level < 0.2

The IF statement

Almost every computer language ever created has an IF statement. It's called a conditional statement in some places, but generally, it looks pretty much like this:

if (boolean-expression is true) then

Why it's a very common computer language feature is because it's a natural way to describe a decision to be made at some point based on data, giving you the means determine what you'd like to do given some data from the real world.

Imagine a gas tank where 0.0 means empty and 1.0 mean the tank is full and of course, 0.5 means the tank is half-full.
So you might see a piece of code like:

if fuel-tank-level < 0.2 then
    refuel-light-is-on = true

Which turns on the refueling light, alerting a driver to get fuel. If the fuel tank is greater than or equal to 0.2, you need not get gas just yet, and the light stays off.

The other VERY common feature of an IF statement is the ELSE clause. It allows you to decide to do one of two things depending on the value of the boolean expression.

if (hour < 18) then
    greeting = "Good day"
    greeting = "Good evening"

/* Depending on the time of day, you change a _greeting_ variable. */

/* or maybe */

if (day is SATURDAY) OR (day is SUNDAY) then
    weekend = true
    weekend = false

/* and then there are a few more wrinkles, like the idea of `else if` (or `elif`) */

if (condition1) then
  /*  block of code to be executed if condition1 is true */
else if (condition2) then
  /*  block of code to be executed if the condition1 is false and condition2 is true */
  /*  block of code to be executed if the condition1 is false and condition2 is false */

And you can usually nest IF statements like this:

if(age >=18)
    print("You are eligible for voting")
    if (age >= 21)
        print("You can legally drink alcohol.")
        printf("But you cannot not drink legally.")
    print("You are not eligible for voting, nor can you drink.")

And the idea of nesting IF statements means there are a few things, if you're not careful, you can get tangled up in, and someday, when you start to run into them, remember this warning - "try to simplify the logic. Having many nested IFs gets you a bad reputation."

The Loop

Have you ever heard the term infinite loop? It turns out an infinite loop is not neccessarily a bad thing. Consider our thermostat example above. You want the thermostat to work all the time no matter what.

five-minutes = 5.0
minutes-run = 0.0
set-point = 72.0

while (true) // <= true NEVER changes, so the loop never quits
    currentTemperature = readRoomTemperature
    if currentTemperature > setPoint
    else if currentTemperature < setPoint
    minutes-run = minutes-run + five-minutes

print-out("We ran for ", minutes-run, "minutes.")

This is important because loops allow us to write programs that repeat certain things and to do things over and over. (Which humans tend to find tedious). When we run this program, it starts out by setting how much time we're going heat or cool or wait for during each cycle of the loop. We then go into an infinite loop. This loop starts at the top, and goes step-by-step until it gets to the bottom of the loop (where it says end-loop) and then the computer goes to the top of the loop and starts over by currentTemperature = readRoomTemperature.

This is our longest program so far, and an important one because to shows a very common pattern of software.
1) we start off setting up a couple of variables
2) we loop forever computing the changes in temperature
3) turning the heating and cooling on and off
4) we NEVER actually print-out anything(!).

Yeah, I know. So we could change the program to be something like this, where we run for a specific period of time.

five-minutes = 5.0
minutes-run = 0.0
set-point = 72.0

while (true)
    currentTemperature = readRoomTemperature
    if currentTemperature > setPoint
    else if currentTemperature < setPoint
    minutes-run = minutes-run + five-minutes

    if minutes-run > 600


print("We ran for ", minutes-run, "minutes.")

Notice how now, when we run for just more than 10 hours (60 times 10 is 600 minutes), as soon as the minutes-run is greater than 600, we will break out of the loop. It's no longer infinite, but it means we have to restart the program every 10 hours.

The general form of loops in many computer languages is usually one of two different kinds. The WHILE loop and the FOR loop.

while (something is true)

for (i = 0, i <= some-limit, i = i + 1)

MANY introductions to programming start you off with a for loop. And they are very handy. The basic idea is that you start with a counter, in this case above, a variable named i which gets incremented by one each time you go through the loop. When the middle boolean expression (i <= some-limit) becomes FALSE, you break out of the loop.
I've always liked the while loop myself, because it lets me imagine looping while something is true.

while (player1isAlive == true)

But you will often see for loops used when something numerical can be said about the loop:

for (i = 0, i < numberOfCustomers, i = i + 1)

Here we loop through all our customers, doing something for each one.

There are a lot of fancy loops, any many forms of loops you will need to get very familiar with. But the key idea here is that loops let you control how many times or under what conditions a computer repeatedly does something useful.

/* sum all the numbers from 0 to `n` */

var sum : integer = 0
var n : integer = 10

for (i = 1, i <= n, i = i + 1)
    sum = sum + i

print("the sum of 1 to ", n, "is", sum)

Up there, the word var is a just a shortcut way to tell the computer I want to use two different variable numbers, n and sum in my program. Each number will vary over the life of the program.

How to keep the code understandable - Functions

The last of the key ideas in programming that we will cover is the idea of a Function. You may remember, again back in algebra or trigonometry, the idea of a function.

x = 9
y = sqrt(x)

We have two variables here, x and y. We set x to 9, and then we set y to the square root of x. when we do this, we end up with y equal to 3. (well, you may not remember square root, but this is true if y is 3 and x is 9: ((y times y) equals square-root-of(x) in math.) So we have 3 times 3 is 9, and 3 is the square root of 9.

Programmers write functions all day long. These days not a lot of square root functions are being written. It only needed to be written once. BUT we can use it over and over once we have written it. (and that's a key idea when we talk about functions) Generally the functions programmers write are more like these examples.

kilometers-to-go = distanceFromCurrentLocationTo(SanFranciso)

listOfRestaurants = findAllRestaurants(within: currentZipCode(), ofKind: mexican)

favoriteCustomerList = Customers().isFavorite(true).sort(byLastName)

Functions are where a programmer decides to create a kind of subprogram, it takes small number of steps and packages them together. Any time you do something more than once in a program, it should probably be a function. Consider this program.

foreach customer in customerList
    if customer.checkingBalance < 0.0 then
        msg = "Dear customer, 
        your account is overdrawn. Send money. 
        Love, the Bank"
        emailtosend = newEmail(to:, 
            subject: "Overdraft Notice", message: msg)
        success = send(emailtosend)
        if success is true then
            print("sent",, "an overdraft notice!")
            print("ERROR: tried to send", 
  , "an overdraft notice! It DID NOT work??")

There is a lot going on here.
Notice here, I've given you a third kind of loop: a foreach loop, where you step through a list of things one by one. In this case, it's a list of customer things, so a different customer is used in each running of the loop.

Sure, if you're careful you can understand it.
But if you break it up into a couple functions, well, it might look like this.

function isBalanceNegative(customer) {
    if customer.checkingBalance < 0.0
        return true
        return false

function sendNastyGram(customer) {
    msg = "Dear customer, 
    your account is overdrawn. Send money. 
    Love, the Bank"
    sendEmail(, "Overdraft Notice", msg)

function sendEmail(to, subject, message) {
    emailtosend = newEmail(to, subject, message)
    if send(emailtosend) is true
        print("sent",, "an overdraft notice!")
        print("ERROR: tried to send", 
  , "an overdraft notice! It DID NOT work??")

foreach customer in customerList
    if isBalanceNegative(customer) is true then

/* maybe more work? */

A lot is still going on here. But what we did was break up a larger problem (the program) into 4 smaller parts (3 different functions and one loop). Do you see how each function has a body? A function body is a list of program steps that are in between braces { } ? That's a common way for a language to make it clear what the list of steps each function should perform as it does its work.

  • a function which checks isBalanceNegative and returns a boolean true or false.
  • a function sendNastyGram which sends an email to a particular customer (we could have other functions that send normal emails).
  • a function sendEmail which can be used to send any kind of email, not just a nastyGram. :-)

And then at the bottom of the program, a foreach loop which steps through the customerList, setting the variable customer to each customer in the list. Then there is an 'if' statement looking at each customer's balance, and 'then' sending a nastyGram if the balance is negative. We tend to refer to using a function as making a function call and when the function is finished with its work, the function returns to the point in the program where the function was called from.

Functions tend to mess up the top-to-bottom idea of a program. Usually you can put functions anywhere in your program and the computer can find them. But, just like a var keyword indicates a new variable, a function keyword declares a new function that can be called from any point to do work for you.

function helloPlayer1() {
    print("Hello, Player1!")
    player1score = 0

That is, be very precise, a function declaration, we are defining a function name 'helloPlayer1'. We can use as many times as we would like. And to use it, you might have a piece of a program that reads like this.


helloPlayer1() /* this is where you 'call' the function */
               /* helloPlayer1 */

while (gameOver is false)

This is where we make a function call to the function 'helloPlayer1', and it will print out a hello, and reset the player1 score to zero. After we return from the helloPlayer1 function, the very next thing that will happen is the 'while' loop.

By breaking the program into some smaller functions, I'd argue (and almost all programmers would agree) that the program is easier to understand and clearer to make changes to in the future.

Guess a Number Game

Here is an example of all these things together. It's a simple number guessing game in Python. I'll leave you to ponder it in light of the things we've discussed. It has some things we've not talked about. But honestly, that's pretty common in this new gig of yours. You read some technical article or program and you have to interpret what you're reading and what it does, even if you're not familiar with the language it is written in.

import random

guesses_made = 0

name = input('Hello! What is your name?\n')

secretnumber = random.randint(1, 20)
print('Well, {0}, I am thinking of a number between 1 and 20.'.format(name))

while guesses_made < 6:

    guess = int(input('Take a guess: '))

    guesses_made += 1

    if guess < secretnumber:
        print('Your guess is too low.')

    if guess > secretnumber:
        print('Your guess is too high.')

    if guess == secretnumber:

if guess == secretnumber:
    print('Good job, {0}! You guessed my number in {1} guesses!'.format(name, guesses_made))
    print('Too bad! Nope. The number I was thinking of was {0}'.format(secretnumber))

This is a sample program that sets up some variables, goes into a loop, asking the user to input number guesses, and checking to see if the guess is right. It also only lets the player guess 6 times before the user loses. You should be able to "get a feel" for what the program does, even if you're not sure what each and every little detail does.


Spend some time looking at each of these key ideas, and what they let you do and think about WHY you'd need to use them.

Now, this is a very opinionated set of ideas I've chosen. I spent no time talking about numeric computation or operators or data structures or algorithms.
Don't worry, we'll cover all the things I left out.
But in my experience with ZipCode groups, the ideas found here are often among the most misunderstood of the early concepts we teach.
Using the theory that if I emphasize these things earlier, you'll be more comfortable with them when you run into them in the course, I've pulled together some things people struggle with when they are under the pressure of a ZipCode cohort.

  • Data types keeping track of strings and numbers (and booleans!)
  • Boolean expressions, a key building block to make decisions in a program.
  • Variables to hold the data you're working with.
  • If conditionals to help make decisions in programs.
  • Loops to enable you to do repetitive things.
  • Functions to make it clearer what you're doing and make your programs more organized.

That's where we leave it for now. Get plenty of rest, you're going to need to be all caught up on your sleep before you start ZipCode.