A1 Worksheet ÔÇô Improve The Code
A1 Worksheet ÔÇô Improve The Code
Code after
1 to_guess = 3
2 not_guessed = True
3 while not_guessed:
4 print("Guess a number")
5 number = int(input())
6 if number == to_guess:
7 not_guessed = False
Tasks .
1. Copy and paste the ‘before’ code into your development environment
2. Edit the code so that the block only has one entry point and one exit point
Note: The program should perform in exactly the same way when executed.
Program 1
Code before
1 def and_function(a, b):
2 if a == True and b == True:
3 return True
4 else:
5 return False
6
7
8 one = 4 == 4
9 two = 2 == 2
10
11 print(and_function(one, two))
Code after
1
2
3
4
5
6
7
8
9
10
11
Program 2
Code before
1 def multiple_five(number):
2 if number % 5 == 0:
3 return "Multiple of 5"
4 else:
5 return "Not a multiple of 5"
6
7 print(multiple_five(12))
Code after
1
2
3
4
5
6
7
8
Program 3
Code before
1 def password_check():
2 password = "12345"
3 entered_pass = ""
4 pass_not_valid = password != entered_pass
5 attempts = 0
6 while pass_not_valid and attempts < 3:
7 print("Enter a password:")
8 entered_pass = input()
Page 3 Last updated: 17-05-21
KS4 - Programming Activity sheet
Lesson 22 - Structured programming
9 attempts = attempts + 1
10 if password == entered_pass:
11 break
12 if password != entered_pass:
13 return "Access Denied"
14 else:
15 return "Access Granted"
16
17
18 print(password_check())
Code after
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.