Lecture 2.3 · Interactive lab

Debugging Lab

Read errors like a detective, catch silent logic errors, and beat the notebook trap

Introduction to Data Science and Machine Learning
Md Mahbub E Noor · University of Barishal
All labs

Error decoder: predict the error, then read it

Each card holds a short program with one problem. Decide which error Python will report before you reveal it. Every message below was produced by running the code in real Python 3.

0 correct

Card 1

Which error will Python report?

What Python prints

Choose an answer to reveal the real output.

What to notice

  • Read the last line first: it names the error type and the problem.
  • The line number points to where Python stopped. A missing bracket or colon is often on that line or the one above.
  • The last card has no error message, yet the answer is wrong. That is a logic error.

Try this

Logic error hunt: no error message, wrong answer

These programs run without complaint. Watch the values inside them to find out why the answer is wrong.

A. Count the vowels

Trace: the values in each round

Expected
0
Program printed
?

B. The average that is too big

On a number line

What to notice

  • count = 1 sets the value; count + 1 calculates and throws the answer away. Only count = count + 1 (or +=) adds.
  • Python does / before +, exactly as in maths. Brackets change the order.
  • Logic errors are found by testing with an example whose answer you already know.

Try this

The notebook trap: Python remembers what has run, not what you see

A mini notebook. Run cells in any order, edit them, restart the kernel, and watch the kernel's memory and the run numbers in brackets.

Kernel memory

Run history

Run cell A, then cell B. Then change A to x = 7 without running it, and run B again.

What to notice

  • [ ] = never run in this session; [3] = the third cell run. The numbers show the real order.
  • An orange dot means the cell was edited after it last ran: the screen and the memory disagree.
  • Restart and run all rebuilds memory from the top, exactly as a fresh reader (or your teacher) would.

Try this

Works offline: no internet needed. The Python shown here runs in your browser with the same rules as Python 3, so results and error messages match what you will see in Jupyter.