design, write and debug programs that accomplish specific goals, including controlling or simulating physical systems; solve problems by decomposing them into smaller parts.
use logical reasoning to explain how some simple algorithms work and to detect and correct errors in algorithms and programs
LO: To know how to use and change a variable.
Wait! You told us a variable was something that stood for a number like n or m in algebra?
Indeed, but sometimes just a part of a truth is easier to understand. Now we can find out that a variable stands for something stored in memory; it can be a number, but also*:
#variables #declare the variable by giving it a name and then assign it a value #a number - an integer num = 1 #a string of words, a word or character - a string #strings differ because they are enclosed in inverted commas message = "hello" #variables can also refer to expressions* answer = (3 * 7) answer2 = num * num
Here we can see that a variable can bee seen as the programs "memory" - what the computer can remember (contrasted with functions - what a computer can do), if we ever want a computer to remember something then we would use a variable.
* Variables can be evaluated from right to left - you work out what is the value on the right side of the = and then assign it to the variable named on the left of the =.
Allow the children to experiment with using variables and the print() function so that they can find out what they can do - and can't do with variables in IDLE interpreter.
#using variables #declare some integer variables num1 = 5 num2 = 7 #declare some string variables word1 = "hello" word2 = "goodbye" #some experiments to try out with the print() function one at a time in IDLE print(num1 * num2) print(num2 - num1) print(word1 * num1) print(word1 + word2) print(word2 + num2) print(word1 * word2)
Let's look at the output from our different experiments:
print(num1 * num2) - the variables evaluate to 5 and 7 so multiplying these together will give 35.
print(num2 - num1) - the variables evaluate to 7 and 5 so subtracting these will give 2
print(word1 * num1) - the variables evaluate to hello and 5, and this sort of makes sense as you are asking for 5 lots of "hello", you will get hellohellohellohellohello. This sounds silly but is actually really important (as we will see later) when debugging code, so watch out for any strange repeating strings!
print(word1 + word2) - the variables evaluate to hello and goodbye so adding these will give hellogoodbye, they have literally been added together, a process known as concatenation (joining strings together.
print(word2 + num2) - the variables evaluate to goodbye and 7 so adding these will give...
...An error TypeError: unsupported operand type(s) for +: 'int' and 'str' or TypeError: can only concatenate str (not "int") to str, basically you can't add a word to a number - who knew?
print(word1 * word2) - the variables evaluate to hello and goodbye so multiplying these together will give an error TypeError: can't multiply sequence by non-int of type 'str'. Notice here that 'Type' refers to the type of variable you are refering to and nothing to do with 'typing' - a mistake in typing is a syntax error and would usually be flagged up as a NameError.
*** There is also a 'float' type of variable for remembering fractional numbers e.g. 1.3. This has been left out here because trying to explain "Floating Point Arithmetic: Issues and Limitations" to 8-9 year olds fills me with dread. We will look at working with decimals later on though.