KS4 - Programming Lesson 20 - Scope Activity sheet Not ‘going global’ Worked example 1 . Program with global variables used 1 2 3 4 5 6 7 8 9 def double(): global num1 num1 = num1 * 2 num1 = int(input()) double() print(num1) Program with a function used instead 1 2 3 4 5 6 7 8 9 Page 1 def double(a): a = a * 2 return a num1 = int(input()) num1 = double(num1) print(num1) Last updated: 17-05-21 KS4 - Programming Lesson 20 - Scope Activity sheet Task . Modify the programs so that they no longer use global variables, but perform the same task. Use the worked example on the first page as a demonstration of this. Program 1 . Program with global variables used 1 2 3 4 5 6 7 8 9 def triple(): global num1 num1 = num1 * 3 num1 = int(input()) triple() print(num1) Converted program (paste your code below) 1 2 3 4 5 6 7 8 9 Make sure that you test your code against the original program to ensure that it works correctly. Page 2 Last updated: 17-05-21 KS4 - Programming Lesson 20 - Scope Activity sheet Program 2 . Program with global variables used 1 2 3 4 5 6 7 8 9 10 def increase_score(): global score if answer == "Yes": score = score + 1 score = 0 answer = "Yes" increase_score() print(score) Converted program (paste your code below) 1 2 3 4 5 6 7 8 9 10 Make sure that you test your code against the original program to ensure that it works correctly. Page 3 Last updated: 17-05-21 KS4 - Programming Lesson 20 - Scope Activity sheet Program 3 . Program with global variables used 1 2 3 4 5 6 7 8 9 def change_colour(): global colour print(f"Your current colour is {colour}") print("What would you like to change it to?") colour = input() colour = "Red" change_colour() print(f"Your current colour is {colour}") Converted program (paste your code below) 1 2 3 4 5 6 7 8 9 Make sure that you test your code against the original program to ensure that it works correctly. Page 4 Last updated: 17-05-21 KS4 - Programming Lesson 20 - Scope Activity sheet Explorer task . Program with global variables used 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 def change_direction(): global direction print(f"Current direction: {direction}") print("Enter new direction") direction = input() def change_speed(): global speed print(f"Current speed: {speed}") print("Enter new speed") speed = input() speed = 10 direction = "North" change_direction() change_speed() print(f"New direction: {direction}") print(f"New speed: {speed}") Converted program (paste your code below) 1 2 3 4 5 6 7 8 Page 5 Last updated: 17-05-21 KS4 - Programming Lesson 20 - Scope Activity sheet 9 10 11 12 13 14 15 16 17 18 19 20 Make sure that you test your code against the original program to ensure that it works correctly. Further explorer task . Write your own mini program ● ● Create a password checker that uses a global variable and convert it into a function If there is time, you could test your program on a partner to see if they can convert it to a function Resources are updated regularly — the latest version is available at: ncce.io/tcc. This resource is licensed under the Open Government Licence, version 3. For more information on this licence, see ncce.io/ogl. Page 6 Last updated: 17-05-21