Skip to main content

Featured

Space Explorers Book 7: Answers and Explanations

Memunic Space Explorers › Book 7: Memoon and the Search for Another Home › Answers and Explanations Answers and Explanations Habitable means life could live there; inhabited means life does live there. A world can be habitable and empty, as an unoccupied flat has a roof and water but nobody in it. The Moon has almost no air, so water would boil or freeze, and its gravity is only about one-sixth of Earth’s, too weak to keep air. Accept also: no greenhouse blanket, so it swings between very hot and very cold. Venus. It matches Earth in size and roughly in mass, but its thick carbon dioxide air traps heat, making it hot and crushing. “Twin” is only true for size and weight. A dip of 1 per cent means (planet width divided by star width) squared is 0.01, so the ratio is 0.1: the planet is about a tenth as wide as the star. Repeats at the same gap show the planet is going round the star in 12 days, and rule out a one-time event such as a passing object. A planet shines only by reflected l...

Byte by Byte Book 4: Chapter 11 – Finding and Fixing Bugs

Byte by Byte › Book 4: Office Apps and First Programs › Chapter 11: Finding and Fixing Bugs

Chapter 11: Finding and Fixing Bugs

You will learn: what bugs are, common kinds of bugs in Scratch, a step-by-step debugging method (reproduce, isolate, hypothesise, fix, test), useful debugging tricks, and how to test a program properly.

🌟 The Adventure

The night before the festival, Memoon ran the whole show one last time. It was a disaster.

The dancer entered, then glided straight off the top of the stage. The lanterns lit up before night fell. The star game started, but the stars fell sideways. And the finale flower drew only one lonely petal before stopping.

Noodles hung his head. While Memoon had been at dinner, he had walked across the keyboard, sat on the mouse, and, by the look of it, played with the code.

"I'm sorry," he mewed. "I made it all wrong."

Memoon took a slow breath and checked her watch. Two hours left. She opened her notebook to a clean page and wrote one word at the top: DEBUG.

"Programmers make mistakes all the time, Noodles," she said. "Even without cats. The trick isn't never making mistakes. It's knowing how to hunt them down."

Illustration from Byte by Byte Book 4: Chapter 11 – Finding and Fixing Bugs

💡 Learn With Memoon

What is a bug? A bug is a mistake in a program that makes it behave differently from what was intended. Debugging means finding and fixing bugs. The words became famous in 1947, when computer scientist Grace Hopper's team found a real moth trapped in a relay of the Harvard Mark II computer and taped it into their logbook as the "first actual case of bug being found". Engineers had already used bug for faults, but the moth made the story legendary.

Every programmer writes bugs. Good programmers are good at debugging.

Common kinds of bugs in Scratch.

Kind of bugWhat goes wrongExample
Sequence bugBlocks in the wrong ordershow placed after glide, so the sprite glides invisibly
Wrong valueA number or choice is wrongpoint in direction 0 (up) instead of 90 (right)
Missing initialisationStarting position or state not setA sprite starts wherever it ended last time
Loop bugWrong count, wrong block inside or outside the loopA turn placed outside the loop, so the flower draws one petal
Condition bugWrong comparison, missing if checked only once instead of inside forever, and versus or mixed upforever
Event or message bugWrong hat block, message name, or broadcast missingLanterns use when green flag clicked instead of when backdrop switches to night
Wrong spriteCode put on the wrong spriteThe star's falling script on the catcher
Timing bugThings happen too fast to see, or in the wrong orderNo wait inside an animation loop

The debugging method. Detectives don't guess wildly; they follow clues. So do programmers.

  1. Reproduce: run the program and make the bug happen again. Note exactly what happens and what should happen.
  2. Isolate: narrow it down. Which sprite? Which script? Which block? Test one script at a time by clicking it directly.
  3. Hypothesise: make a guess about the cause: I think the turn is outside the loop.
  4. Fix: change one thing at a time. If you change five things at once, you won't know which one mattered.
  5. Test: run it again. Did it fix the bug? Did it break anything else? If not fixed, undo and try your next hypothesis.

Debugging tricks.

  • Click a script to run just that script, or a single block to test it alone.
  • Add say blocks to show what's happening inside: say (x position) or say Reached the loop! This is like writing notes to yourself; programmers call it print debugging or tracing.
  • Slow it down: add wait 1 seconds between steps so you can watch each one.
  • Tick the checkbox next to a reporter such as x position or direction to show its live value on the stage.
  • Rubber duck debugging: explain your code out loud, block by block, to someone (or a toy duck). Hearing yourself explain it often reveals the mistake.
  • Compare with your plan: check the script against your pseudocode or flowchart.
  • Save versions: before big changes, File → Save to your computer with a new name (show v2), so you can go back.

Testing. A program isn't finished when it works once. Testing means running it in different situations to find bugs before your users do. Test:

  • Normal cases: what usually happens (the star is caught).
  • Edge cases: the limits (the catcher at the very edge of the stage, a quiz answer of 0, clicking very fast).
  • Unexpected input: what a real user might do (typing six instead of 6, pressing a key nobody planned for).

Ask someone else to try it; they will find things you never thought of.

Illustration from Byte by Byte Book 4: Chapter 11 – Finding and Fixing Bugs

✍️ Try It Yourself

Activity 1: Bug hunt. Each script below has one bug. Build it in Scratch, run it, follow the five-step method, then fix it.

Bug 1 (dancer should enter from the left and glide to centre):

when green flag clicked
glide 2 secs to x: 0 y: -100
go to x: -200 y: -100
show

Bug 2 (should draw a hexagon):

when green flag clicked
erase all
pen down
repeat 6
    move 80 steps
turn ↻ 60 degrees

Bug 3 (star should go back to the top whenever it touches the catcher):

when green flag clicked
if ⟨touching Catcher?⟩ then
    go to x: (pick random -220 to 220) y: 180
forever
    change y by -5

Bug 4 (lantern should appear only at night):

when green flag clicked
show
forever
    next costume
    wait 0.5 seconds

Bug 5 (quiz should say Correct for 8):

ask (What is 4 + 4?) and wait
if ⟨(answer) > 8⟩ then
    say Correct!
else
    say Try again.

Activity 2: Say-block detective. In your star game, add say (y position) inside the star's forever loop. Watch the numbers fall. At what y value does the star restart? Remove the say block afterwards.

Activity 3: Test plan. Write a test plan for your star game in a table in Word: Test, What I did, What should happen, What happened, Pass/Fail. Include at least two normal cases and two edge cases. Then run your tests.

Unplugged: Buggy directions. Write directions from your bedroom to the kitchen with one deliberate bug (a wrong turn or a missing step). Give them to a family member to follow exactly. Can they find the bug? Use the five steps.

🎉 Memoon Solves It

Memoon worked through the list, one bug at a time. Reproduce: the dancer flew upward. Isolate: she clicked only the dancer's script; still upward. Hypothesise: wrong direction? She ticked the direction checkbox: 0. Noodles had changed point in direction 90 to 0. Fix one number. Test: the dancer entered from the left. One down.

The lanterns were using when green flag clicked; she changed it back to when backdrop switches to night. The stars had change x instead of change y. The flower's turn block had been dragged outside the inner loop. Each time she changed one thing and tested again.

Then she ran the whole show. Perfect. To be sure, she tested the edge cases: Beautine pressed against both edges of the stage, the announcer spoke for twice as long, Bop pressed f three times in a row. Everything held.

Noodles' ears lifted. "You fixed my mess."

"We fixed it," said Memoon, handing him the rubber duck. "You're on duck duty. Every programmer needs one."

❓ Check Your Knowledge

  1. What is a bug? What is debugging?
  2. What happened in 1947 with Grace Hopper's team?
  3. List the five steps of the debugging method in order.
  4. Why should you change only one thing at a time when fixing a bug?
  5. How can a say block help you debug?
  6. What is rubber duck debugging?
  7. What is the difference between a normal case and an edge case? Give an example of each for a quiz program.
  8. True or false: if a program works once, it has no bugs.
  9. Name the kind of bug in Bug 2 above, and explain the fix.
  10. Challenge: A sprite should say Hello and then move, but it moves first. Write two different possible causes (hypotheses) and how you would test each one.

📚 Word Bank

  • Bug: a mistake in a program that makes it behave wrongly.
  • Debugging: finding and fixing bugs.
  • Reproduce: make a bug happen again on purpose.
  • Isolate: narrow down where a bug is.
  • Hypothesis: an educated guess about the cause of a problem.
  • Print debugging (tracing): showing values while a program runs to see what's happening.
  • Rubber duck debugging: explaining code out loud to find mistakes.
  • Testing: running a program in different situations to find bugs.
  • Edge case: a test at the limits of what a program should handle.
  • Version: a saved copy of a project at one point in time.

Keep learning with Byte by Byte

Explore Book 4: Office Apps and First Programs. Find the ebook, purchase details and teaching slides on MemuniCity.

Watch this chapter on YouTube

Comments

Popular posts from this blog

CA-7 Job Status and Failure Reason Codes

Here are the various status codes that You may see for jobs in the Queue or Logs in CA-7. R-C0002 R-C0003 R-C0099 R-C0255 R-JCLER A-U0002 A-U0004 A-U0015 A-U0043 A-U4095 A-SOC7 A-U4000 Reason Code/Job Status Code explanation: The indication of the job's current disposition. Values and their meanings are: A-Snnnn-- The job has failed with a system abend. A-Unnnn --The job has failed with a user abend. C-Cnnnn --The job has completed successfully and is going through completion processing. E-ARFATT-- An error occurred during the attempt to attach ARF definitions. This can result if the ARFSET specified is not defined. A diagnostic message may have been written to the CA-7 log, the OS console, or to the trailer queue for the job (in which case it can be displayed using LQ,JOB=x,LIST=ALL). E-ARFERR ---The job encountered an error in the ARF processing. Do a LARFQ command for the job to get more specific information. E-SEGERR --The job has been requeued...

Whistling Winds and Waterfalls: 4 Days of Magic in Shillong

Nestled in the clouds, where the scent of pine trees mingles with the rhythm of waterfalls and the tunes of local guitar-strumming youth—Shillong is more than a place. It’s a vibe. If you’ve got 3 nights and 4 days, you’ve got just enough time to fall in love. Here’s how to make the most of it—food, fun, fog and fantastic views included. Day 1: Shillong, We’re Coming for You! You land in Guwahati, barely out of the airport when the hills start calling. You hop into your cab, Spotify on, windows down, and the three-hour ride to Shillong begins. Just when you think it can’t get better, you stop at Umiam Lake —a lake so calm, it looks like someone ironed the water. By evening, you’re sipping hot chai in Laitumkhrah, maybe nibbling on spicy momos or pork jadoh from a roadside stall. You’re checked into your charming hotel ( Tripura Castle if you fancy old-world royalty, Ri Kynjai if you’re into lakeside poetry). The breeze whispers secrets of waterfalls and pine forests. Sleep well, drea...

Installing and Configuring Hadoop on Ubuntu as a Pseudo Distributed Single Node Cluster

Briefly what is psuedo-distributed single node Hadoop cluster: It is a Hadoop distributed cluster having Single Node. All the five daemons (NN,DN,JT,TT,SNN) are running as individual JVM (java process) instances. We will get all distributed features of Hadoop. This mode is used to develop, test, and staging (before production) Map Reduce programs quickly. It writes all the system, daemon and user logs to different files on the cluster To install Hadoop in this mode, we have to modify the hadoop-env.sh, core-site.xml, hdfs-site.xml, mapred-site.xml. hadoop-env.sh contains all the Environment variables which are required for running Hadoop. All the default values are fine, Only we have to modify the JAVA_HOME=“The location of Java Home Directory” Core-site.xml contains all the cluster specific information like io, temp directory, sorting, file system uri etc. Hdfs-site.xml contains all the parameters which are specific to HDFS like name node directory, data node directory...