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.