Cambridge Lower Secondary Computing 8 Ben Barnes Margaret Debbadi Pam Jones Tristan Kirkpatrick SERIES EDITOR: Lorne Pearcey SAMPLE MATERIAL We are working with Cambridge Assessment International Education to gain endorsement for this forthcoming title. Please note this is a sample and not a full chapter We are working with Cambridge Assessment International Education to gain endorsement for this forthcoming series. Boost eBooks – interactive, engaging and completely flexible Boost eBooks use the latest research and technologies to provide the very best learning experience for students. They can be downloaded onto any device and used in the classroom, at home or on the move. l Interactive: Packed with features such as notes, links, highlights, bookmarks, formative quizzes, flashcards, videos and interactive revision. l Accessible: Effortlessly support different learning styles with text-tospeech function. l Flexible: Seamlessly switch between the printed page view and interactive view. Created with teachers and students in schools across the globe, Boost is the next generation in digital learning for schools, bringing quality content and new technology together in one interactive website. The Cambridge Lower Secondary Computing Teacher’s Guides include a print handbook and a subscription to Boost, where you will find a range of online resources to support your teaching. l Confidently deliver the new curriculum framework: Expert author guidance on the learning models and approaches plus a suggested Scheme of Work. l Develop key concepts and skills: Suggested activities, quizzes and guidance on assessment, as well as ideas for supporting and extending students working at different levels. l Enrich learning: Audio versions of the texts to help aid understanding, pronunciation and critical appreciation. To explore the entire series, visit www.hoddereducation.com/cambridge-lowersec-computing We’re here to help! If we can help with questions, and to find out more, please contact us at international.sales@hoddereducation.com Contents Introduction 8.1 Computer systems: The inside track 8.2 Networks and communication: Across the world in an instant 8.3 Making choices: Search and select 8.4 Testing conditions: Developing games 8.5 Click and collect: Data collection and validation 8.6 Software development: Testing Glossary Index Comp_L_Sec_Sample.indd 1 8/12/21 2:07 PM Unit 8.4 Testing conditions: Developing games Get started! Have you ever played a game where you had to make choices? Discuss the following with a partner: l What sort of decisions did you make in the last game you played? l Did you notice that the game progresses differently, depending on which choices you made? l Have you every played a game that contained bugs? Computer programs need to be able to check user choices and other conditions. They also need to be tested to make sure that the program runs correctly for everyone. In this unit, you will use Python to develop and test computer games that run different sections of code, depending on the user’s input. Learning outcomes In this unit, you will learn to: l create and follow a flowchart that uses conditional statements l develop programs in Python that use conditional statements l understand how AND, OR and NOT can be used in algorithms l develop programs in Python that use AND, OR and NOT l develop programs in Python using different data types l develop programs using an iterative process l develop and apply a test plan l understand the need for using a range of test data l test a program using suitable test data. Warm up In pairs, think about your journey to school this morning. What conditions did you or your parents have to check before leaving home? For example: l Have you eaten any food? l Have you brushed your teeth? l Is your schoolbag packed? l Are the doors locked? Make a list of all the conditions you can both think of. Most computer programs also check conditions as they are running. The conditions depend on what the program does. In this unit, you will see how a game, written in Python, checks conditions to allow a player to move through it. You will also see how important it is to test the game, so that it works correctly for every possible choice. 4 Comp_L_Sec_Sample.indd 4 8/12/21 2:07 PM Unit 8.4 Testing conditions: Developing games SCENARIO Younger children need to understand the importance of secure passwords. Your school wishes to promote online safety and, as a programmer, you have been asked to create a text adventure game for young children aged 8–10 years old. The game should provide the children with some different pathways to follow as they progress through a story. The child will play the main character in the game. They need to find the Chocolate Room in a sweet shop. They will meet a robot and the sweet-shop owner on their journey. When they find and enter the Chocolate Room, there will be two questions. If the child answers the questions correctly, they will be given three letters from a four-letter password. They then have to guess the password, after being given a clue. If they guess the password correctly, they open a digital treasure chest and win the game. Your challenge is to examine existing program code and find out what it does, correct errors in the code and complete the game. You need to use the different sample code provided by your teacher. You will add new code, edit code and test any changes made to the code to make sure that it works correctly. DID YOU KNOW? KEYWORDS The first adventure games for computers were text adventure games. This was because the computers were not powerful enough to display good graphics. A famous example is the game Zork, which you can still play online today. Search for one of the early Zork games online to find out the quality of the graphics computers of that time could display. text adventure game: a game in which the player types in commands to control the main character program code: the Python code created in the IDLE arithmetic operators: +, – , *, / and other symbols that can be used for arithmetic Do you remember? Before starting this unit, you should be able to: create an algorithm using flowchart symbols understand the logic of AND, OR and NOT understand the difference between integer, real and string data types know how to develop programs in Python with inputs and outputs use variables in Python use arithmetic operators in Python to debug a program. 5 Comp_L_Sec_Sample.indd 5 8/12/21 2:07 PM Cambridge Lower Secondary Computing Conditional statements Learn Sometimes decisions have to be made in a program. This is usually because the program has reached a point where there is more than one option or choice. For example: In the adventure game, players must enter their name and age, and then (if over 8 years-old), they will enter a name for the sweet-shop owner. After this, there are a number of possible pathways for the program to follow, depending on how the player answers certain questions. Selection allow a program to check whether certain conditions have been met. This allows the program to select different outcomes. Selection is important in programming, because the programmer can offer the user choices about the way in which they move through the program. Selection is achieved by using IF statements. The structure of an IF statement Here are two simple IF statements: if playerage>10: Condition 1 is that playerage is greater than 10. print(“You are too old to play this game”) if playerage<8: Condition 2 is that playerage is less than 8. print(“You are too young to play this game”) This statement uses two conditions and two IF statements. Remember logical operators: Equal to = Not equal to != Less than < Greater than > Less than or equal to <= Greater than or equal to >= Remember: A condition contains a logical operator. The logical operator combines the parts of a condition together. For example: playerage>10 is a condition. The condition can evaluate to true or false. IF statements can be extended to IF—ELSE statements, which allow for more than one outcome. When a condition in an IF statement evaluates to true, the indented statements following IF are executed (but the statements following ELSE are not executed). When a condition in an IF statement evaluates to false, the indented statements following ELSE are executed (but the statements following IF are not executed). 6 Comp_L_Sec_Sample.indd 6 8/12/21 2:07 PM Unit 8.4 Testing conditions: Developing games KEYWORDS For example, this IF—ELSE statement allows for two different outcomes: if (playerage>=8: print(“You can play this game”) else: print(“You cannot play this game”) Here is a flowchart representing the solution. You can see the two possible paths: 1 one for ‘Yes’, when the condition is true and the statements following IF are carried out selection: selecting statements based on a decision condition: an expression that evaluates to true or false; for example, is age <10? indentation: move the Python code four spaces to the right 2 one for ‘No’, when the condition is false and the statements following ELSE are carried out. Playerage >= 8 Yes Print ‘You can play this game’ No Continue with the rest of the program Print ‘You cannot play this game’ End program The exact way of writing IF—ELSE statements in Python is as follows: if condition: #statements to be #carried out when the #condition is true else: #statements to be #carried out when the #condition is false Note the indentation used in the lines following if and else. Python uses indentation so that it knows which statements belong with if and which belong with else. You must include both the colons in an IF–ELSE statement in Python. Python will indent the statements automatically after you type the ‘:’ symbol. If the indentation is not correct, you will get an error, because Python needs to know which statements to group together. 7 Comp_L_Sec_Sample.indd 7 8/12/21 2:07 PM Cambridge Lower Secondary Computing Practise l Open the file indentfile.py provided by your teacher. l The file has errors because the indentation of the print statements is incorrect. Select the print statements as shown. l l l l Use the dedent option to bring the print statements back to the correct position. Save the program. Run the program. In a text adventure set in space, the main character is an astronaut exploring the caves of Mars. The player notices a door on the right of the main cave. The game asks the player whether or not they wish to go through the door. Draw a flowchart to show how the game can give the player the option to either open the door or carry on walking through the main cave. DID YOU KNOW? KEYWORD You can use the indent function on Python’s menu to highlight code and indent it all together. dedent: move the Python code out by four spaces If you want to remove the indent, you can highlight the code and use the dedent feature. This will move all highlighted code out by four spaces. 8 Comp_L_Sec_Sample.indd 8 8/12/21 2:07 PM Unit 8.4 Testing conditions: Developing games Combining conditions using AND and OR Learn Decisions sometimes need to be made based on two or more conditions. These conditions can be combined in different ways. Remember: AND and OR are called Boolean operators. OR needs only one of the conditions to be true to execute the statement that follows. AND needs all of the conditions to be true to execute the statement that follows. Condition 1 OR Condition 2 Condition 1 AND Condition 2 Example 1: if you help the robot in the adventure game with directions to the Digital Sweet Shop OR help him with his calculations, then you can have some sweets. Example 2: if you help Botty with directions to the Digital Sweet Shop AND help him with his calculations, then you can have some sweets. What is the difference between the way in which conditions are combined in Example 1 and Example 2? l In Example 1, you only need to do one of the two tasks to receive the sweets. l In Example 2, you must do both tasks to receive the sweets. IF statements in program code can be combined using logical operators such as AND and OR. Look at the following IF statement in Python. It is more efficient than the previous examples, as two conditions have been combined into one. if (playerage>=8 and playerage<=10): print(“You can play this game”) This IF statement is made up of two conditions. The print statement is carried out only when both condition 1 (playerage>=8) AND condition 2 (playerage<=10) are true. Using the AND operator will give an overall outcome of true if both condition 1 and condition 2 are true; otherwise it gives an overall outcome of false. Here is another version of an IF statement that uses two conditions combined with the OR operator. Using the OR operator will give an outcome of true if either condition 1 or condition 2 is true; otherwise it gives an overall outcome of false. 9 Comp_L_Sec_Sample.indd 9 8/12/21 2:07 PM Cambridge Lower Secondary Computing This table shows when the print statement will be executed by the program when the user inputs a few different ages. Player age entered Print statement executed 10 7 You are not the correct age to play this game! Sorry!! 9 20 You are not the correct age to play this game! Sorry!! 6 You are not the correct age to play this game! Sorry!! 12 You are not the correct age to play this game! Sorry!! Multiple conditions can also be used within IF—ELSE statements. For example, this IF—ELSE statement allows for two different outcomes: if (playerage>=8 and playerage<=10): print(“You can play this game”) else: print(“You cannot play this game”) IF condition 3: statements to be carried out when both conditions 1 and 2 are true. ELSE: statements to be carried out when either condition 1 or condition 2 are false. This means that if condition 1 is true but condition 2 is false, the ELSE statement is executed. Similarly, if condition 2 is true but condition 1 is false, the ELSE statement is executed. If condition 1 is false and condition 2 is false, then the ELSE statement is executed. Practise l Look at these IF statements relating to the playerage: if playerage>10 or playerage <6: print(“You can play the game”) else: print(“You cannot play the game”) l Using the IF statements above, copy and complete the table by writing which print statement will be executed for each of the player ages entered. The first one has been completed for you. Player age entered Print statement executed 10 You cannot play the game 7 9 20 6 4 l Discuss with a friend how this IF statement will change the age rules for playing the game. l Create a new IF statement, which will ensure that players aged 7 to 11 can play the game. Use the following partially completed IF statement to help you. if playerage >=7 and _________________________: print _________________________ else: print _________________________ 10 Comp_L_Sec_Sample.indd 10 8/12/21 2:07 PM Unit 8.4 Testing conditions: Developing games Computational thinking Look at the code below on the left. Discuss with a partner what the output would be if you enter the following values: x=4, y=7; x=8, y=8; x=11, y=21. Look at the code below on the right. Which message will be output if you enter the following values: x=5, y=4, z=11 x=7, y=7, z=99 x=12, y=21, z=0 Create a set of values for x, y and z so that the message ‘Hello world’ is output. Go further Once the player has answered the robot’s question, they enter the Chocolate Room, where they find the sweet-shop owner. She will ask the player a question, as follows: Which of the following could be used as a good password? 1. Your pet’s name 2. Password123 3. A random set of numbers and letters. The correct answer is 3. If the player enters 3, they can then pick one of two chocolate bars by entering a number 1 or 2. l If the player picks chocolate bar 1, there is no information in it and they lose a life. l If the player picks chocolate bar 2, the letter ‘T’ is inside the wrapper. If the player gives the wrong answer (1 or 2) to the owner’s password question, they lose all their chocolate and lose a life. 11 Comp_L_Sec_Sample.indd 11 8/12/21 2:07 PM Cambridge Lower Secondary Computing Some of the code for the Chocolate Room has been completed for you. Look at the code with a partner. Your challenge is to add the lines of code that are missing for the Chocolate Room. You can do this by opening the file ad62.py provided by your teacher and add the required lines of code. Ending the game Once the player has left the Chocolate Room, the program will: l ask the player to try to remember any of the letters they were given in the game l ask the player to try to guess the password using the clue and the letters they already know l calculate and show the player their score. l Add the lines of code that are missing from this section of the program. You can do this by editing ad63.py provided by your teacher. l Run the program and correct any errors. l 12 Comp_L_Sec_Sample.indd 12 8/12/21 2:07 PM Unit 8.4 Testing conditions: Developing games Challenge yourself! Your challenge is to write a new text adventure game. The game is set in a series of tunnels and the object of the game is for the player to find a key in one of the tunnels and then to find their way to the exit. The exit is blocked by a gate that is unlocked by the key. The game has the following features: The player always walks forwards through the tunnels — they can’t go backwards. l The first tunnel ends at a junction. At the first junction, the player can either take the left tunnel, the middle tunnel or the right tunnel. l Each of those tunnels also ends in a junction where the player can go left, middle or right. This means that there are up to nine possible destinations in the game. Only one of those destinations is the exit with the gate. l One of the tunnels contains the key for the exit. You must design the game so that the player can reach the exit from the tunnel that has the key — otherwise the game can never be won! l The junction containing the final exit must test whether the player has selected the correct direction and if they have the key in order for them to win the game. l Some of the other tunnels can have a range of different features, such as human characters, monsters, dead-ends or hazards. You can design these features as you wish — be imaginative! l The player must pick up points every time they move through a tunnel, but they can also lose points — for example, if they set off a trap. The game should update their score using a variable and give them their score at the end. l The game should ask for the player’s name at the start of the program and then address them by their name throughout the game. If the player does not enter a name, then the code containing the rest of the game should not execute. You should design your game using a flowchart algorithm before writing any code. Once you have written your program, you should write a test plan and then test the game using suitable test data. 13 Comp_L_Sec_Sample.indd 13 8/12/21 2:07 PM Cambridge Lower Secondary Computing Final project The skills you have learned in this unit were all in the context of a text adventure game. However, these skills can be used in any kind of program that you write. In this project, you will use your skills to develop and test a quiz game. You are now going to create a new game for use with 10 to 11 year-olds to test their mental arithmetic. Players will be asked to enter two numbers to multiply together, and then asked to give the answer. The program will tell them if they are right or wrong. You will use all the skills you’ve learned in this unit to create the game using Python. Before you write any code, create a flowchart algorithm based on the details listed below. When you are sure that your flowchart fully covers all the requirements of the game, write the code for the game in Python. Your game should have the following features: l An introductory screen with information about the game l A section of code to allow the player to input their name and age, which must be between 10 and 11, as older or younger players are not allowed to play l A section of code to print a welcome message and the player’s details on the screen l A section of code that asks the player which two numbers they wish to multiply together, and which then stores the first number in a variable called number1 and the second number in a variable called number2, as real numbers l A section of code that prints out number1 * number2 as an equation, with a question mark, for example: What is 5 * 5? l A section of code to calculate the result of multiplying the variable number1 by number2, using use an assignment to store the result of this calculation in a variable called result l A section of code to allow the user to input their answer to the question, stored in a variable called answer, and ensuring that the user’s input is stored as an integer l A section of code, containing an IF statement, which checks to see if the answer equals the result; if the result and the answer are the same, the player’s score increases by 10, and the following message displays: ‘Correct — well done! You get 10 points!’ l A section of code that checks to see if the answer is within 1 of the result; if so, then the player’s score increases by 1, and this message displays: ‘Incorrect — but you were close! Have 1 point for trying and better luck next time.’ For example: If the result was 20, then an answer of 19 or 21 would display this message. A section of code that displays the following message for all other incorrect answers: ‘Incorrect — you have scored 0 points.’ l A section of code to output the player’s score and a message stating that the program has ended. Run your game and correct any errors. l Once you have run your program, you need to test it. l Create a test plan for your program. l Select a range of test data and, for each case, explain why you chose it. l Test your program using this test data. Record any further errors you discover before correcting your program. 14 Comp_L_Sec_Sample.indd 14 8/12/21 2:07 PM Unit 8.4 Testing conditions: Developing games Evaluation l Swap programs with a partner and play each other’s game. Comment on the following: Does it cover all of the requirements? l Is the game easy to use? l Are the messages easy to understand? l Are the scores correct for a range of different multiplications? (You should check the calculations and keep a written record of each answer to see if the code is working correctly.) Now open your own program and look at the code. Reflect on what could be improved in your game. Things you might want to think about include the following: l What are the largest numbers that you’d expect 10 to 11 year-olds to be able to multiply in their head? l How easy would it be to change the program so that it could test addition and subtraction? Based on the evaluations, make a list of recommendations to improve your game. l l l What can you do? Read and review what you can do. I can create and follow a flowchart that uses conditional statements. I can develop programs in Python that use conditional statements. I can understand how AND, OR and NOT can be used within algorithms. I can develop programs in Python that use AND, OR and NOT. I can develop programs in Python using different data types. I can develop programs using an iterative process. I can develop and apply a test plan. I can understand the need for using a range of test data. I can test a program using suitable test data. Registered Cambridge International Schools benefit from high-quality programmes, assessments and a wide range of support, so that teachers can effectively deliver Cambridge Lower Secondary. Visit www.cambridgeinternational.org/lowersecondary to find out more. Photo credits: Cover © AndSus/Adobe Stock Photo; p. 4 tr © MC Stock/Adobe Stock Photo; p. 4 cr © Max/Adobe Stock Photo; p. 5 tr © Hor/Adobe Stock Photo; p. 8 cr © Klyaksun/Adobe Stock Photo; p. 10 tr © Kirsty Pargeter/Adobe Stock Photo; p. 12 tr © Destina/Adobe Stock Photo; p. 13 cc © Hadeev/Adobe Stock Photo. t = top, b = bottom, l = left, r = right, c = centre Hachette UK’s policy is to use papers that are natural, renewable and recyclable products and made from wood grown in well-managed forests and other controlled sources. The logging and manufacturing processes are expected to conform to the environmental regulations of the country of origin. Orders: please contact Hachette UK Distribution, Hely Hutchinson Centre, Milton Road, Didcot, Oxfordshire, OX11 7HH. Telephone: +44 (0)1235 827827. Email education@hachette.co.uk. Lines are open from 9 a.m. to 5 p.m., Monday to Saturday, with a 24-hour message-answering service. You can also order through our website: www.hoddereducation.com © Margaret Debaddi, Siobhan Matthewson 2022 First published in 2022 by Hodder Education, an Hachette UK Company, Carmelite House, 50 Victoria Embankment, London EC4Y 0DZ www.hoddereducation.com Impression number 10 9 8 7 6 5 4 3 2 1 Year 2026 2025 2024 2023 2022 All rights reserved. Apart from any use permitted under UK copyright law, no part of this publication may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying and recording, or held within any information storage and retrieval system, without permission in writing from the publisher or under licence from the Copyright Licensing Agency Limited. Further details of such licences (for reprographic reproduction) may be obtained from the Copyright Licensing Agency Limited, www.cla.co.uk Typeset in FS Albert 12/14 by IO Publishing CC Printed in the UK A catalogue record for this title is available from the British Library. 15 Comp_L_Sec_Sample.indd 15 8/12/21 2:07 PM Lower Secondary Computing 8 Deliver an exciting computing course for ages 11–14, building on students’ existing computing skills and experience whilst demonstrating new concepts, with practice opportunities to ensure progression. ● Recap and activate students’ prior knowledge with ‘Do you remember?’ panels and introduce more advanced skills with ‘Challenge yourself!’ tasks. ● Allow students to demonstrate their knowledge creatively with engaging end of unit projects that apply skills and concepts in a range of different contexts. ● Develop computational thinking with an emphasis on broadening understanding throughout the activities. ● Provide clear guidance on e-safety with a strong focus throughout. This resource is endorsed by Cambridge Assessment International Education al E m bridge A ss on Ca 25 ducation W For over 25 years we have been king for ove or r trusted by Cambridge schools around the world to provide YEARS quality support for teaching and i es learning. For this reason we have sm WITH rnat e n t In t e been selected by Cambridge Assessment International Education as an official publisher of endorsed material for their syllabuses. Provides support as part of a set of resources for the Cambridge Lower Secondary Computing curriculum framework (0860) from 2022 Has passed Cambridge International’s rigorous quality-assurance process Developed by subject experts For Cambridge schools worldwide This title is also available as an eBook with teacher support. Visit hoddereducation.co.uk/boost to find out more. We are working with Cambridge Assessment International Education to gain endorsement for this forthcoming series.