C. Algebra · Patterns, Equality and CodingReading and Changing Code: Fixing BugsC3.2About 35 minutesLearning goalsI can read code and tell what will happen.I can find a bug (a mistake) in code.I can change code to fix it or to make something new happen.I can explain how changing the code changes what happens.Words to knowbugA mistake in code that makes it do the wrong thing.debugTo find and fix the bug in code.alterTo change something.outcomeWhat happens at the end; the result.obstacleSomething in the way that you must go around.Uh oh, a bug!Sometimes code does not do what we want. A mistake in code is called a bug. Finding and fixing the bug is called debugging.Every coder finds bugs. Even grown-up coders who make video games and apps! Debugging is a big part of coding, and it is like solving a puzzle.How to debugRead the code, one step at a time.Run it with your finger or with objects.Find the step where it goes wrong.Change that step.Run it again to check.Beep starts in the bottom-left square. The star is 4 squares to the right and 2 squares up. The dashed line shows where the code below takes Beep.START➡ Move right 3⬆ Move up 2STOPThis code has a bug. Beep stops one square to the left of the star.Worked exampleDebug Beep's codeThe code is Move right 3, Move up 2. Beep ends one square left of the star. Fix the bug.Read the code and run it with your finger: right 1, 2, 3. Up 1, 2.Beep is in the right row, but one square too far left.So the problem is how far Beep moves right. Count the squares from Beep to under the star: 1, 2, 3, 4.Change Move right 3 to Move right 4.Run it again: right 4, up 2. Beep lands on the star!The fixed code is ➡ Move right 4, ⬆ Move up 2. Changing 3 to 4 made Beep go one more square to the right.Watch out for obstacles!Now there is a pond in the way. Beep cannot swim! Look at the grid.~Beep starts in the bottom-left square. The pond is in the bottom row, 3rd square. The star is 3 squares to the right and 2 squares up.START➡ Move right 3⬆ Move up 2STOPThis code would make Beep go into the pond! Splash!Worked exampleFix it by changing the orderChange the code so Beep goes around the pond and still reaches the star.Run the code: Beep moves right along the bottom row. Square 1 is fine, but square 2 is the pond. That is the bug.What if Beep goes up first? Then Beep is no longer in the bottom row.Swap the order of the two steps: Move up 2 first, then Move right 3.Run it: up 2 (Beep is now 2 rows higher), right 3 (Beep moves along the row with the star). No pond in that row!The fixed code is ⬆ Move up 2, ➡ Move right 3. Changing the order of the steps changed Beep's path, so Beep goes around the pond.START⬆ Move up 2➡ Move right 3STOP ⭐The fixed code. The steps are the same, but the order is different.Changing code changes the outcomeWe can also change code on purpose, to make something new happen. Watch what happens to the number when we change one step.CodeRun itOutcomeStart with 10, Add 2, Add 210 → 12 → 1414Start with 10, Add 5, Add 510 → 15 → 2020Start with 10, Add 5, Add 5, Add 510 → 15 → 20 → 2525Start with 20, Add 5, Add 520 → 25 → 3030Changing the numbers, adding a step, or changing the start all change the outcome.Worked exampleDescribe the changeCode A: Start with 10, Add 2, Add 2. Code B: Start with 10, Add 5, Add 5. How did the change affect the outcome?Run Code A: 10 → 12 → 14.Run Code B: 10 → 15 → 20.We changed Add 2 to Add 5. Adding a bigger number each time makes a bigger total.20 is 6 more than 14 (each step added 3 more, and there are 2 steps: 3 + 3 = 6).Changing Add 2 to Add 5 made the outcome bigger: 20 instead of 14.Worked exampleA bug in the orderHere is code for making a snowman. What is the bug?Put on the hatRoll a big ballRoll a middle ball and put it on topRoll a small ball and put it on topRead and run each step in your head.Step 1 puts on the hat, but there is no snowman yet. There is no head to put it on!The hat must go on after the small ball (the head) is on top.Move Put on the hat to the end.The fixed code: 1. Roll a big ball. 2. Roll a middle ball and put it on top. 3. Roll a small ball and put it on top. 4. Put on the hat.Your turn1The star is 2 squares right and 3 squares up from Beep. The code is Move right 2, Move up 2. What is the bug?Show answerBeep only moves up 2, so it stops one square below the star. Fix it: Move up 3.2The code Move right 4 makes Beep go one square past the star. How do you fix it?Show answerChange it to Move right 3, one less square.3Code: Start with 0, Add 10, Add 10. What is the outcome? Change it so the outcome is 30.Show answerOutcome: 20. Add one more step, Add 10, to get 30.4Code: Start with 5, Add 5, Add 5. Change only the start number so the outcome is 20.Show answerStart with 10: 10 → 15 → 20.5Code for a sandwich: 1. Eat the sandwich. 2. Put cheese on bread. 3. Put bread on top. What is the bug?Show answerYou can't eat it first! Move Eat the sandwich to the end: put cheese on bread, put bread on top, eat the sandwich.6Code: Start with 30, Take away 10. Outcome is 20. What happens if you change Take away 10 to Add 10?Show answerThe outcome becomes 40 instead of 20. Adding makes it bigger; taking away made it smaller.Just for funA true story: in 1947, people working on a giant computer found a real moth stuck inside it! They taped the moth into their notebook and wrote that they had found an actual bug.Hands-on activityBug huntYou need: the floor grid from the last lesson (tape, chalk or tiles), a toy, a pillow (the obstacle) and paper.A grown-up writes code for you to follow, but puts one bug in it on purpose.Be the robot and follow the code exactly.Where did you end up? Find the bug!Change the code by crossing out the wrong step and writing a new one.Run it again to check.Now put the pillow in the path. Change the code so the robot goes around it.Switch: you write code with a sneaky bug for your grown-up to find.Be a good debuggerWhen you find a bug, say what went wrong and why your change fixes it. For example: "Beep stopped too early, so I made the number bigger."More practice: warm-up1Read the code: Start with 5, Add 5. What is the outcome?Show answer10. 5 → 10.2True or false? A bug is a mistake in code.Show answerTrue. Finding and fixing it is called debugging.3The star is 3 squares to the right of Beep. The code says Move right 2. What happens?Show answerBeep stops one square before the star. Fix it: Move right 3.4Code: Start with 10, Add 10. Change Add 10 to Add 20. What is the new outcome?Show answer30. 10 + 20 = 30. (Before the change it was 20.)5Code for playing in the snow: 1. Go outside. 2. Put on your boots. What is the bug?Show answerThe order is wrong! You need your boots before you go out. Fixed code: 1. Put on your boots. 2. Go outside.More practice: on your own1The star is 4 squares up from Beep. The code says Move up 5. What is the bug? How do you fix it?Show answerBeep goes one square past the star. Change it to Move up 4, one less square.2Code: Start with 0, Add 5, Add 5, Add 5. What is the outcome? Change the code so the outcome is 10.Show answerOutcome: 15 (0 → 5 → 10 → 15). To get 10, take away one Add 5 step: 0 → 5 → 10.3Code: Start with 40, Take away 10, Take away 10. What is the outcome? What happens if you change the start to 50?Show answerOutcome: 20 (40 → 30 → 20). Starting with 50: 50 → 40 → 30. Starting 10 higher makes the outcome 10 higher.4The star is 3 squares right and 1 square up from Beep. The code is Move right 3, Move down 1. What is the bug?Show answerThe direction is wrong in step 2. Beep goes down instead of up, so it ends 2 squares below the star. Fix it: Move up 1.5Code: Start with 2, Add 2, Add 2. The outcome is 6. Change the code so it counts by 2s all the way to 10.Show answerAdd two more Add 2 steps: Start with 2, Add 2, Add 2, Add 2, Add 2. Run it: 2 → 4 → 6 → 8 → 10.Challenge1Beep starts in the bottom-left square. A pond is in the bottom row, in the 2nd square. The star is 2 squares right and 1 square up. The code is Move right 2, Move up 1. What goes wrong? Fix it.Show answerBeep's first step right lands in the 2nd square, which is the pond. Splash! Change the order: ⬆ Move up 1, ➡ Move right 2. Now Beep goes up first and moves along the row above the pond to the star.2Code A: Start with 10, Add 10, Add 10. Code B: Start with 20, Add 10. Do they have the same outcome? Explain.Show answerYes, both are 30. Code A: 10 → 20 → 30. Code B: 20 → 30. Code B starts 10 higher but has one less Add 10 step, so the outcome is the same.3This code has two bugs. Beep needs to go 2 squares right and 3 squares up. The code is ⬅ Move left 2, ⬆ Move up 4. Find and fix both bugs.Show answerBug 1: Beep goes left instead of right. Change it to ➡ Move right 2. Bug 2: Beep goes up one too many squares. Change it to ⬆ Move up 3. Fixed code: ➡ Move right 2, ⬆ Move up 3.Check your understandingQuiz1. What is a bug in code?An insectThe last step of the codeA mistake that makes the code do the wrong thing2. Beep needs to go 5 squares right. The code says Move right 4. How do you fix it?Add Move up 1Change it to Move left 4Change it to Move right 53. Code: Start with 10, Add 10, Add 10. If you take away the last step, what is the new outcome?1020304. A pond is in Beep's path. What could you change in the code?Nothing, Beep can swimErase all the codeThe order of the steps, so Beep goes around the pond5. Code: Start with 15, Add 5. What happens if you change Add 5 to Take away 5?The outcome changes from 20 to 25The outcome stays 20The outcome changes from 20 to 106. The star is 2 squares up from Beep. The code says ⬇ Move down 2. What is the bug?The direction is wrongThe number is wrongThere is no bugMark lesson completeFor parents and teachersReading and changing ("altering") code teaches children to predict, test and revise, which is the heart of problem solving in math. It also builds a healthy attitude to mistakes: a bug is just something to fix.Common mistakesRewriting all the code instead of finding the one wrong step. Encourage running the code step by step and stopping where it goes wrong.Fixing by guessing. Ask your child to say why a change will work before testing it.Not noticing that the order of steps is the bug. Act out the steps physically to make order problems obvious.How to helpDeliberately give silly, buggy instructions ("Put on your shoes, then your socks") and let your child debug them.Praise persistence and explaining: "You found the bug and told me why. That's what real coders do!"Ask: What would happen if we changed this number? What if we added one more step?PreviousCoding: Step-by-Step InstructionsNextPatterns, Equality and Coding: Unit Review