1.3. Problem Solving, Interactivity & Ethics

This is a textbook that is designed to teach you how to program, but threaded throughout the book is three strands: problem solving (programming through the design of algorithms), interactivity (programming for people), and ethics (programming responsibly). We briefly introduce these three ideas here.

1.3.1. Problem Solving with Algorithms

If problem solving is a central part of computer science, then the solutions that you create through the problem solving process are also important. In computer science, we refer to these solutions as algorithms. An algorithm is a step by step list of instructions that if followed exactly will solve the problem under consideration.

For example, an algorithm to compute the area of a circle given its radius might look like this:

Algorithm Example 1 (English)

  1. Ask for radius

  2. Compute area by squaring radius and multiplying the result by pi

  3. Display the computed area

Notice that this algorithm consists of a set of numbered steps. It is written in English, for ease of understanding. Although simple algorithms are easily understood when written in English, more complicated algorithms need more precise notation. For improved precision, algorithms are often written in pseudocode. Pseudocode is a notation that is more precise than English but generally not as precise as a programming language. The same algorithm expressed in pseudocode might look something like this:

Algorithm Example 2 (Pseudocode)

  1. Ask for radius

  2. let area = (radius2) × π

  3. Display area

Note how the pseudocode example expresses step 2 more precisely, specifying the formula in mathematical terms.

Our goal in computer science is to take a problem and develop an algorithm that can serve as a general solution. Once we have such a solution, we can use our computer to automate its execution using programming. Programming is a skill that allows a computer scientist to take an algorithm and represent it in a notation (a program) that can be followed by a computer. A program is written in a programming language such as Python, the language you will learn in this book.

To help you understand the difference between an algorithm and a program, consider this program which computes the area of a circle (hit the ‘Save & Run’ button to see it execute):

A program is an algorithm expressed in a programming language. We might also say that a program is an implementation of an algorithm. In this example, both the algorithm and the program have three steps. The first step (on line 1) gets some input from the user and then turns the input into something the computer can do math with; the second step (line 2) performs a calculation using the information obtained in the first step; and the final step (line 3) displays the result to the user or programmer. Even though we haven’t covered any details of Python, hopefully you can see the correspondence between the steps of the algorithm, which could be followed by a human (but not executed by a computer), and the steps of the program, which can be executed by a computer (which we ran by clicking ‘Save & Run’).

Algorithms are important because the process of solving a problem through programming often begins by designing an algorithm. The programmer often expresses the algorithm in pseudocode, then converts the algorithm to a program for the computer to execute. In the next section, you will learn how to execute Python programs on a computer.

1.3.2. Interactivity - Programming for People

Consider the code we introduced in the previous section. The program is interactive because it asks for input from an end user. The end user is a person who might use your program, but will never see the code or programming instructions that you write. They just interact with the program. While you work through this book, you get to wear two hats: you get to act as both the programmer and the end user. The input() command on line 1 of the program above causes Python to show a dialog box to the end user, and the end user can type in a number. When you edit the code in the activecode window, you are wearing your programmer hat. When you type a number into the dialog box that comes up, you are wearing your end user hat.

Let’s take a look at a version of this program that is not interactive:

If you run this program, no dialog box pops up, and the user does not get to provide any input at all. Instead, there is a hard-coded value for the radius: 15. That means that this program can only calculate one thing: the area of a circle with a radius of 15. That’s pretty boring and not very useful! Adding interactivity to our programs make them more dynamic and useful. The output of many of the programs in this book is displayed in the console - that’s the grey output window underneath the activecode window. The console is where errors and output messages appear and that is something a programmer or technology specialist uses. Most of the time, end users don’t see what is printed to the programmer’s console. In most modern applications, end users will interact with a program through user interface controls and windows. We will get to that as you progress through this book, but often we will have output print to the console, just to keep things simple while you are learning.

A program that is not interactive (that does not get input from a user while the program is running) is called a batch program. Sometimes batch programs read input from a file, or sometimes data is hard-coded right into the program, like the value 15 in the example above. Batch programs are used for data processing in many big companies. And often, when people are learning how to program, the first programs are batch programs because they are often simple: get some input, do something with it, spit out a result. Interactive programs are more complicated, because they often involve waiting for users to do things, and then doing things in response. However, interactive programs are really common in real life: every app that you use on your phone, your laptop, your smart watch, your video game console, or your fitness tracker is an interactive program.

In this book we will teach you some of the basic elements of interactivity and interactive programming. We will show you how you can design interfaces that show the output of your programs to end users on a screen by popping up message boxes, drawing images on a canvas that the user can interact with, or presenting graphical user interfaces that have buttons and menus that a user can interact with. These interactivity elements will be interspersed throughout the book alongside more traditional batch programs that just output information to the console.

1.3.3. Ethics - Programming Responsibly

You may be wondering why there is a section on ethics in your programming book. You might be thinking that this is where we tell you it is wrong to cheat on your programming assignments. That is true, but that is not what this section is about. Ethics is critically important in computer science. People who are trained in computer science go on to develop technologies that change the world. Technologies impact so much of our day to day life, and so it is critical that as you learn how to design, develop and deploy technology, you make decisions so that the technology you put out in the world does not cause harm. That may seem obvious, but it’s not as easy as you think to predict how the technology you develop might be used and abused. Throughout this book we will ask you to pause and consider the technological implications of your choices as a programmer. By the end of this course, we hope you will recognize the important of ethics in computer code.

Even simple programs like the one above embed ethical values. You may be thinking “What?!? There are no ethics involved in calculating the area of a circle!”. But consider this version of the same program:

In this version of the program, we have rounded the value of PI down to a whole number. After all, 3 is quite close to 3.1415, right? Now you may be thinking, that’s not an ethical decision - that’s just an error. But the original value of 3.1415 isn’t the true value of PI either. If you are a math afficionado, you will recall that PI has many, many digits beyond 3.1415. So, a programmer in the first program made a decision that four digits after the decimal number is enough precision. They made a decision to leave off many digits. In the second version of the program, we’ve just made a different decision about precision. The decision of level of precision is made by the programmer, and it could have disastrous consequences. What if we are trying to design a part for an airplane and because of this, a part is made the wrong size and that causes a malfunction which leads to a plane crash? One of the things we will discuss throughout this book is the many ways that programs embed ethical values, because programs are written by humans. It’s very important to self-reflect on how your values are reflected (or not) in a program you write.

Check your understanding

You have attempted of activities on this page