Year 8 · Summer 2

Raid: App Forge · Lesson 3 of 5

Events and logic: buttons, variables and conditions

PracticalTopic 13 Programming basics

Year 8 · Raid: App Forge · Lesson 3 of 5

You will learn to

  • Explain what event-driven means: code runs when something happens, like a button click.
  • Use a when Button.Click block to set a label's text.
  • Use a global variable to count the clicks.
  • Use an if block to change the message when the count reaches 10.

Year 8 · Raid: App Forge · Lesson 3 of 5

Success criteria

You have got it when you can say:

  • I can make a label change when I press a button.
  • I can show the number of clicks on the screen.
  • I can make the message change at exactly 10 clicks.
  • I can explain how this is different from Python, which runs from top to bottom.

Year 8 · Raid: App Forge · Lesson 3 of 5

Retrieval starter

Questions from before. Write your answers, then show them.

  1. Last lesson

    Testing a 0–50 range with 50 is…

    A Boundary dataB Erroneous dataC Normal data

    A. Boundary data

  2. Last unit

    Binary search only works if the list is…

    A Made of numbersB SortedC Short

    B. Sorted

  3. Earlier this year

    A program runs with no message but says the average of 4 and 6 is 7. This is a…

    A Runtime errorB Syntax errorC Logic error

    C. Logic error

Year 8 · Raid: App Forge · Lesson 3 of 5

Key words (1 of 2)

variable
A named place in memory that holds a value.
assignment
Storing a value in a variable. = in Python, ← in pseudocode.
Casting
Changing a value from one data type to another, like int("13").
Concatenation
Joining strings together: "7" + "7" = "77".

Year 8 · Raid: App Forge · Lesson 3 of 5

Key words (2 of 2)

Selection
Choosing a path with if / else.
Iteration
Repeating steps: a loop.
Boolean
A value that is only True or False.
Infinite loop
A loop whose condition never becomes false.

The explanation · step 1 of 1 · part 1 of 2

Variables: labelled boxes

A variable is a named box in memory that holds a value.

score = 0
score = score + 10
print(score)
  1. score = 0: make a box called score, put 0 in it. This is assignment.
  2. score = score + 10: read the right side first. 0 + 10 = 10. Put 10 back in the box.
  3. print(score) shows 10.

The explanation · step 1 of 1 · part 2 of 2

Variables: labelled boxes (continued)

!Watch out

In code, = means 'store', not 'equals'. To ask if two things are equal, use ==.

e.g.Example

Scratch: the orange 'set score to 0' and 'change score by 10' blocks.

Year 8 · Raid: App Forge · Lesson 3 of 5

Quick quiz · 1 of 3

What does 17 % 5 give?

  1. A 3
  2. B 2
  3. C 3.4

B. 2

17 ÷ 5 = 3 remainder 2. % gives the remainder.

Year 8 · Raid: App Forge · Lesson 3 of 5

Quick quiz · 2 of 3

range(3) gives…

  1. A 0, 1, 2
  2. B 0, 1, 2, 3
  3. C 1, 2, 3

A. 0, 1, 2

Starts at 0, stops before 3.

Year 8 · Raid: App Forge · Lesson 3 of 5

Quick quiz · 3 of 3

x = input("Number: ") and the user types 5. What type is x?

  1. A float
  2. B str
  3. C int

B. str

input() always gives a string: "5". Cast it with int() to do maths.

Year 8 · Raid: App Forge · Lesson 3 of 5

Exit ticket

Answer on paper before you go. Then check.

  1. How many times does for i in range(2, 10, 2): run?

    A 8B 5C 4

    C. 4 2, 4, 6, 8. The next would be 10, which is the stop, so it isn't used. 4 times.

  2. score = 45. Code: if score >= 25: print("Bronze") elif score >= 40: print("Gold"). What prints?

    A BronzeB GoldC Both

    A. Bronze 45 >= 25 is True, so the first branch runs and the rest is skipped. The order is wrong: a logic error.