Functions and structured code · GCSE · OCR J277 2.2.3, AQA 8525 3.2.10, Edexcel 1CP2 6.6.3 · about 15 min
Scope: where a variable exists, and why passing values beats global.
[1 mark]A function sets gap = 10 inside its body. After calling it, the program runs print(gap). What happens?
[1 mark]What does this program print?
speed = 80
def gentle():
speed = 30
print(speed)
gentle()
print(speed)[1 mark]beeps = 0 outside, and inside a function beeps = beeps + 1 with no global line. What happens when the function runs?
[1 mark]Which are reasons local variables are good practice?
Tick every answer that is true.
[1 mark]What does this program print?
count = 0
def add():
global count
count = count + 1
add()
add()
print(count)[1 mark]What is the scope of a variable?
This program should beep three times and print beeps: 3, but it crashes. Fix it, with parameters and return or with global.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
beeps = 0
def beep():
tone(880, 0.1)
wait(0.1)
beeps = beeps + 1
beep()
beep()
beep()
print("beeps:", beeps)Plan your program here, then type it in and press Run.
longest_so_far(reading, best) that returns whichever is larger, and use it in a loop of readings without any global variables.NOTE = 660 and use it inside beep. Why does reading it work without global?