0% found this document useful (0 votes)
20 views5 pages

A1 Worksheet ÔÇô Improve The Code

The document provides examples of code before and after refactoring to improve the structure. It contains three programming tasks with code snippets to refactor by having single entry and exit points. The goal is to make the code more readable and maintainable while keeping the same functionality.

Uploaded by

joasebr08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

A1 Worksheet ÔÇô Improve The Code

The document provides examples of code before and after refactoring to improve the structure. It contains three programming tasks with code snippets to refactor by having single entry and exit points. The goal is to make the code more readable and maintainable while keeping the same functionality.

Uploaded by

joasebr08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

KS4 - Programming Activity sheet

Lesson 22 - Structured programming

Improve the code


Worked example .
Code before
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 break

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

Page 1 Last updated: 17-05-21


KS4 - Programming Activity sheet
Lesson 22 - Structured programming

3. Paste the improved code into the ‘after’ box

Note: The program should perform in exactly the same way when executed.

Tip: Use the worked example above as a guide.

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

Page 2 Last updated: 17-05-21


KS4 - Programming Activity sheet
Lesson 22 - Structured programming

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

Page 4 Last updated: 17-05-21


KS4 - Programming Activity sheet
Lesson 22 - Structured programming

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.

Page 5 Last updated: 17-05-21

You might also like