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,
assign and concatenate. 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 assignment? Click here. Need more help with concatenation? Click here.
first_name = "Andy" print(first_name)
Predict what this outputs, then run it. Which of the two lines stores something, and which one shows something?
The first two lines are the ones you already met in example 1, repeated here so that this block runs on its own. The new work happens on the third line.
first_name = "Andy" last_name = "Colley" full_name = first_name + " " + last_name print(full_name)
Predict what this outputs, then run it. Pay close attention to the " " in the
middle of the concatenation, and to the fact that full_name is assigned the
result of an expression rather than a value you typed out yourself.
The same variables again, but this time they are concatenated into a longer sentence.
first_name = "Andy"
last_name = "Colley"
full_name = first_name + " " + last_name
print("Hello " + first_name + ". Your full name is " + full_name + ".")
Predict what this outputs, then run it. Count the pieces that are being concatenated, and work out exactly where each space and each full stop in the output comes from.
Make sure that you check for the following things:
= for assignment.+ symbol to concatenate (join).+ symbols and the variable names are not surrounded by speech marks.