In a predict and run task you do not write code from scratch. You start from a small piece of working code, work out what you think it will do before you run it, and then run it to check whether you were right.
This task contains three examples. Work through them in order, because each one builds on the one before it.
For every example, study the code carefully and write down a prediction of what
it will do when it runs. Your prediction should use the key terms variable,
operator and output. You can type your prediction straight into the code
as a comment, on a line starting with #.
Only once you have written your prediction should you run the code and compare the result with what you expected.
Need more help with arithmetic operators? Click here. Need more help with casting? Click here.
This code performs arithmetic calculations on fixed numbers.
print(8 + 2) print(8 - 2) print(8 * 2) print(10 / 4) print(10 // 4)
Predict what this outputs, then run it. The last two lines divide the same two numbers with two different operators: work out what each of them does with the part after the decimal point.
The same kind of calculation, but the numbers are stored in variables instead of being typed into the sum.
num1 = 20 num2 = 5 result = num1 * num2 print(result)
Predict what this outputs, then run it. Three variables are used here, and only one of them is output. Work out which line does the calculating and which line does the outputting.
This time one of the numbers comes from the user, so it has to be cast before it can be used in the calculation.
print("Enter a number:")
num1 = int(input())
num2 = 2
result = num1 // num2
print(result)
Predict what this outputs, then run it and type 9 when it asks you for a
number. Then run it again with 10, and again with 2. Explain why the answer
changes the way it does, and what the int() around the input() is there for.
Make sure that you check for the following things:
= for assignment.+ to add, - to subtract, * to
multiply, / to divide with a decimal result, and // to divide and keep
only the whole number part.input.