{"assignment":{"_schema_version":2,"course_id":37,"date_created":"2022-06-28T19:00:00+00:00","date_modified":"2025-08-17T17:49:38.982590+00:00","extra_instructor_files":"","extra_starting_files":"","forked_id":null,"forked_version":null,"hidden":false,"id":996,"instructions":"## What is Debugging?\n\n![On the left, a picture of an actual insect (a bug) taped to a piece of paper that reads \"(moth) in relay, First actual case of bug being found\".  On the right, a picture of [Admiral Grace Hopper](https://en.wikipedia.org/wiki/Grace_Hopper) (one of the most famous computer scientists)](functions_debugging_bugs.png)\n\nWhen a program doesn't work, we say that it has a \"bug\" in it.\nWhen we get rid of bugs, we call it _debugging_.\nThe first actual case of debugging a computer program was when Admiral Grace Hopper (pictured below) found a moth trapped inside of a relay of the Mark II Computer.\nNowadays, our bugs usually don't involve live insects.\n\n## Tracing Code\n\n```python trace-code\ndef calculate_volume(height: int, area: int) -> int:\n    volume = height ** area    # should be 150\n    return volume\n\nbox_length = 5\nbox_width = 10\nbox_height = 3\nrectangle_area = box_length * box_width\nbox_volume = calculate_volume(box_height, rectangle_area)\nprint(box_volume)\n```\n\nOne of the useful features that a modern programming environment like BlockPy offers is the ability to trace a program using an integrated debugger.\nThis tool allows you to slow down the execution of a program so you can walk through the steps line by line.\nAt each step of the code, the editor will show you the actual values of all the variables in the program.\nThis makes it much easier to figure out what your program is doing and shows you when the program has done something you did not want.\nIf you run the code shown here, then a \"View Trace\" button will appear in the Feedback area. You can then step through the program one step at a time.\n\n## Example Code Trace\n\n1. Line 1: Define `calculate_volume` function\n2. Line 5: Assign `box_length`\n3. Line 6: Assign `box_width`\n4. Line 7: Assign `box_height`\n5. Line 8: Assign `rectangle_area`\n6. Line 9: Call `calculate_volume`\n7. Line 2: Assign `volume`\n8. Line 3: Return `volume`'s value\n9. Line 9: Assign `box_volume`\n10. Line 10: Print `box_volume`'s value\n\nLet's trace through the program defined before. Notice that the steps do not match up to the lines!\n\nStart by defining the function `calculate_volume` with two parameters: `height` and `area`. The return type is specified as `int`. We skip over the body of the function (lines 2 and 3) for now.\n\nAfter the function definition (starting on line 5), define and initialize three variables: `box_length` with a value of `5`, `box_width` with a value of `10`, and `box_height` with a value of `3`.\n\nNext, calculate the `rectangle_area` by multiplying `box_length` and `box_width`. In this case, it should be `5 \\* 10`, which evaluates to `50`.\n\nThen, call the `calculate_volume` function with the arguments `box_height` and `rectangle_area`. This means that the `height` parameter will receive the value of `box_height` (`3`) and the `area` parameter will receive the value of `rectangle_area` (`50`).\n\nInside the `calculate_volume` function, the `volume` variable will be assigned the result of `3 \\* 50`. This calculates the volume by multiplying the `height` by the `area`. In this case, the expression will evaluate to `150`.\n\nThe `volume` (`150`) is then returned from the function and assigned to the variable `box_volume`.\n\nFinally, the value of `box_volume` (`150`) is printed to the console.\n\n## Tracing in BlockPy\n\n![A screenshot of the BlockPy interface, with the previously described code shown. The code has been executed, and the View Trace button has been clicked. The current values of the variables are shown as of step 7, and line 2 of the program is highlighted](bakery_functions_debugging_trace.png)\n\nBlockPy's integrated debugger is a convenient tool for stepping through your code and inspecting variable values at each step.\nNotice that as you step, the currently executed line of code is highlighted in green.\nThe type and values of variables are shown in the grey box.\n\nThis tool is similar to tools in other environments like Thonny, although each environment has its own unique features and interface.\nYou should learn to explore the debugging tools available in your specific environment.\nNever be afraid to experiment.\n\n## The Scientific Method\n\n1. Observe failure.\n2. Hypothesize cause.\n3. Predict behavior.\n4. Experiment with code.\n5. Repeat 3-4 until fixed.\n\nWhen you're debugging code, you should approach it using the scientific method.\nFirst, we observe a failure in our code.\nSecond, develop a hypothesis as to why the code failed.\nThird, use this hypothesis to propose a change and predict its effect.\nFourth, test the prediction by experimenting with the code and making observations.\nFifth, repeat steps 3 and 4 until you have fixed the code.\nThis may be intuitive to some people, but formally following this method can really help train yourself as a programmer!\n\n## Wolf Fence Debugging\n\n```python wolf-fencing\ncommand = input(\"What are the dimensions\")\nstop = \"stop\" in command\n#------------------------------------------\nprint(\"Works fine until here\")\n#------------------------------------------\narea = length ** width\nvolume = height * area\nset_size(volume)\n```\n\n\"There's one wolf in Alaska.\nHow do you find it? First, build a fence down the middle of the state, wait for the wolf to howl, determine which side of the fence it is on.\nRepeat the process on that side only until you get to the point where you can see the wolf.\nIn other words, put in a few \"print\" statements until you find the statement that is failing (then, work backwards from the \"tracks\" to find out where the wolf/bug comes from.)\" This method is also known as _print_ _debugging_.\"\n\n## Rubber Duck Debugging\n\n![Person speaking to a rubber duck, saying: \"Here it should increase the sum variable. Oh wait, it's decreasing it. My mistake. Thanks rubber duck!\"](functions_debugging_duck.png)\n\nOkay, this will sound weird.\nFirst, get yourself a rubber duck or a wise stuffed animal.\nSecond, politely inform the duck that you will be explaining your code to it.\nThird, explain what your code is supposed to do in general, and then go over each line of code with the rubber duck.\nAt some point, you will tell the rubber duck what you are supposed to be doing next, and you will realize that's not actually what your code does.\nFourth, thank the duck for its excellent work.\nThis may sound crazy, but rubber ducks are the world's best coders.\n\nThe actual reason why this tends to work is that explaining your code out loud to someone else forces you to pay closer attention to what the code does.\nJust reading the code to yourself allows you to skip over mistakes.\nBut talking to a rubber duck requires your full attention!\n\n## Summary\n\n- The process of removing errors (\"bugs\") from code is called debugging.\n- A debugger is a feature in many programming environments and can be used to trace through code, tracking values of variables, to determine when an incorrect operation occurs.\n- The scientific method is an iterative process of hypothesizing what an error means, predicting a potential fix based on the hypothesis, and then experimentally trying the fix to see if the error is gone.\n- Wolf Fence debugging is when you repeatedly divide the codebase in half with `print` statements until an error is isolated.\n- Rubber Duck debugging is when you explain your code to an inanimate object (like a rubber duck) so that you are forced to pay close attention to your code, hopefully making the error more visible.\n\n## More About Rubber Ducks\n\n[https://rubberduckdebugging.com/](https://rubberduckdebugging.com/)\n\n[https://www.smbc-comics.com/comic/the-rubber-duck-method](https://www.smbc-comics.com/comic/the-rubber-duck-method)","ip_ranges":"","name":"2A6) Debugging Functions Reading","on_change":"","on_eval":"","on_run":"","owner_id":1,"owner_id__email":"acbart@udel.edu","points":0,"public":true,"reviewed":false,"sample_submissions":[],"settings":"{\n  \"header\": \"Debugging Functions\",\n  \"slides\": \"bakery_functions_debugging.pdf\",\n  \"youtube\": {\n    \"Bart\": \"GOcrBbukqlw\",\n    \"Amy\": \"Cqqi7jla8U8\"\n  },\n  \"video\": {\n    \"Bart\": \"https://blockpy.cis.udel.edu/videos/bakery_functions_debugging-Bart.mp4\",\n    \"Amy\": \"https://blockpy.cis.udel.edu/videos/bakery_functions_debugging-Amy.mp4\"\n  },\n  \"summary\": \"In this lesson, you will learn some practical techniques for fixing a program that has errors. These techniques are known as \\\"debugging\\\", and can be difficult to learn - you will get better with them as you practice.\",\n  \"small_layout\": true\n}","starting_code":"","subordinate":true,"tags":[],"type":"reading","url":"bakery_functions_debugging_read","version":8},"ip":"216.73.216.157","submission":{"_schema_version":3,"assignment_id":996,"assignment_version":8,"attempts":0,"code":"","correct":false,"course_id":37,"date_created":"2026-05-20T14:01:58.893846+00:00","date_due":"","date_graded":"","date_locked":"","date_modified":"2026-05-20T14:01:58.893846+00:00","date_started":"","date_submitted":"","endpoint":"","extra_files":"","feedback":"","grading_status":"NotReady","id":2036940,"score":0.0,"submission_status":"Started","time_limit":"","url":"submission_url-cc7aea47-a7af-4ada-b107-ee5e7b0ea0f4","user_id":2044668,"user_id__email":"","version":0},"success":true}
