Simple coding
Simple coding

Start this free course now. Just create an account and sign in. Enrol and complete the course for a free statement of participation or digital badge if available.

2.1 The art of failure

The following details are important once you start writing code. Computers are not as smart and accommodating as human readers: at the slightest spelling mistake (like pint instead of print) or missing punctuation (forgetting the comma or double-quotes, for example), the computer will give up on understanding and running your code and will display an error message.

On top of that, most error messages are rather cryptic. Hence it’s best to get used to them, by doing errors on purpose, so that you have a clue what might be wrong when you are writing your own code.

Activity 1

Put double-quotes around the 7 on the first line and run the program. You will get an error message saying that there is a type error. This basically means the code is mixing data of different types, mixing apples and oranges if you like. In this case, we are trying to add a number (3) to a string ("7") which of course doesn’t make sense. Click on the X at the right end of the message to make it go away and then reset the program to undo the change.

Sometimes, code has more subtle errors, that lead to unexpected results. This is akin to miscommunication, when other people understand something different from what you intended to say. Here is an example.

Activity 2

Put double-quotes around the variable name in the second line. Run the program. Try to explain what happened before reading further.

When putting double-quotes around the variable name, it becomes a string. Hence, the computer will print the name literally, instead of printing the value stored in the variable.

To sum up, "result", "7"result and 7 are different things: the first two are strings (literal text), the third is a variable, and the last is a number.

Learning from mistakes is always helpful, so for this and the remaining programs in this tutorial, I encourage you to introduce errors and observe what message or output you get. Don’t forget to reset the program after each error.

In a sequence, all instructions are executed. Next you will learn how to selectively execute some instructions but not others.

Take your learning further

Making the decision to study can be a big step, which is why you'll want a trusted University. The Open University has 50 years’ experience delivering flexible learning and 170,000 students are studying with us right now. Take a look at all Open University courses.

If you are new to University-level study, we offer two introductory routes to our qualifications. You could either choose to start with an Access module, or a module which allows you to count your previous learning towards an Open University qualification. Read our guide on Where to take your learning next for more information.

Not ready for formal University study? Then browse over 1000 free courses on OpenLearn and sign up to our newsletter to hear about new free courses as they are released.

Every year, thousands of students decide to study with The Open University. With over 120 qualifications, we’ve got the right course for you.

Request an Open University prospectus371