In a predict, run and investigate 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, run it to check whether you were right, and then take the code apart to see how each piece contributes to the result.
Study this code carefully, but do not run it yet.
print("Hello World!")
Write down a prediction of what this code will do when it runs. Use the key
terms run, output and string in your prediction. You can type your
prediction straight into the code above as a comment, on a line starting with
#.
Once you have written your prediction, run the code and compare the result with what you expected.
Then predict what each of the pieces of code below would do. Answer each question for yourself first. You can check your prediction by typing the code into the example above and running it. Only then open the answer.
What would the output of the code print("I love Computing") be?
The output is the text inside the speech marks, without the speech marks themselves:
I love Computing
What would happen if the code print("I love Comping") was run?
It runs perfectly well, and outputs:
I love Comping
Python never checks whether the text inside a string makes sense. Anything between the speech marks is just text to be output, spelling mistakes included. The syntax is correct, so there is no error, but the program does not do what you meant it to do.
What would happen if the code print("I love Computing" was run?
Nothing is output at all. The closing bracket is missing, so the syntax is
wrong, and Python refuses to run the program. Instead it reports a
SyntaxError telling you that the opening bracket was never closed.
This is the difference with the previous question: a spelling mistake inside a string gives you the wrong output, but a mistake in the syntax means you get no output whatsoever.
What would the output of this code be?
print("I can't wait")
print("to learn to code")
The code outputs two lines:
I can't wait
to learn to code
Each print starts a new line of output. The second string does not carry on
from the first one on the same line, even though the two strings together make
one sentence.
Answer each question for yourself first. You can edit the code above and run it to check. Only then open the answer.
Which part of the example code is the output function?
print is the output function. It is the instruction that tells the computer to
present something to the user.
Which part of the example code is the string?
"Hello World!" is the string: the text between the speech marks, together with
the speech marks that mark where it starts and ends.
Make sure that you check for the following things:
print does not have any capital letters." ".( ).