Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

In an investigate task you take working code apart to see how each piece contributes to the result. Answer every question for yourself first. You can edit the code above the question and run it to check. Only then open the answer.

Example 1

f_name = "Mr"
l_name = "Colley"

print(f_name)

Identify a variable in the code.

Answer

f_name and l_name are both variables. Each of them is a name on the left of an =, so each of them is a box that has a piece of data assigned to it.

Identify a string in the code.

Answer

"Mr" and "Colley" are both strings: text between speech marks. They are the data that gets assigned to f_name and l_name.

The line l_name = "Colley" is changed to l_name = "Thorpe". How does this affect the output?

Answer

Not at all. The output is still:

Mr

The value inside l_name really does change, but l_name is never used again. Only f_name reaches a print, so only f_name can affect what appears on the screen. Assigning a value to a variable is not the same as outputting it.

The line f_name = "Mr" is changed to f_name = "Mrs". How does this affect the output?

Answer

The output changes to:

Mrs

print(f_name) outputs whatever is inside f_name at that moment, so changing what was assigned to it changes the output too. Notice that the print line itself did not have to be edited.

Example 2

num1 = 20
num2 = 5

total1 = num1 + 15
total2 = num2 * 2
total3 = total1 - total2

print(total3)

What will be output by the program?

Answer
25

Work through the assignments one line at a time, always using the values the variables hold at that point:

  • total1 = num1 + 15, and num1 is 20, so total1 becomes 35.
  • total2 = num2 * 2, and num2 is 5, so total2 becomes 10.
  • total3 = total1 - total2, so total3 becomes 35 minus 10, which is 25.

Note that + and * here work on numbers, not on strings, so they add and multiply instead of joining text together.

Example 3

name1 = "Ross"
name2 = "Monica"
name3 = "Joey"
name4 = "Rachel"
name5 = "Chandler"

print(name1 + " and " + name4)
name3 = "Phoebe"
print(name3)

What will be output by the program?

Answer
Ross and Rachel
Phoebe

The first print concatenates the contents of name1, the string " and " and the contents of name4. The second print runs after name3 has been given a new value, so it outputs Phoebe rather than Joey.

How many variables are used in the program?

Answer

Five variables are created: name1, name2, name3, name4 and name5.

Only three of them actually do anything, though. name1, name4 and name3 reach a print, while name2 and name5 are assigned a value that is never used again. Creating a variable costs nothing, but a variable nothing ever reads also achieves nothing.

What would be the impact of changing print(name1 + " and " + name4) to print(name1 + " and " + name5)?

Answer

The first line of output would become:

Ross and Chandler

name5 contains "Chandler", so that is what gets concatenated in place of "Rachel". The second line of output is unaffected.

What is the purpose of the + symbol in print(name1 + " and " + name4)?

Answer

It concatenates: it joins the three pieces into one single string that is then output. The pieces are the contents of name1, the fixed text " and ", and the contents of name4.

The spaces around the word and come from inside that fixed text. Without them the output would read Rossand Rachel.

The line print(name3) has been added to the end of the code. Explain what it does.

Answer

It outputs the contents of name3 as they are at that point in the program, which is Phoebe.

That is the interesting part: name3 was assigned "Joey" at the start, but the line above reassigned it to "Phoebe". A variable only ever holds the value it was given most recently, and the earlier value is gone for good.

Help! My code doesn't work!

Make sure that you check for the following things:

  • print does not have a capital letter.
  • The string text is surrounded with speech marks.
  • The string (including the speech marks) is surrounded by brackets.