Debugging in Python, part 5: Working through multiple bugs

MP 144: What happens when you fix a bug, only to find another bug right away?

Note: This post is part of an ongoing series about debugging in Python. The posts in this series will only be available to paid subscribers for the first 6 weeks. After that they will be available to everyone. Thank you to everyone who supports my ongoing work on Mostly Python.

We often think of debugging as fixing a bug, when in reality there's usually a number of bugs we have to work through before things start working again. In the previous examples in this series, fixing one bug brought the project to a working state again. In this post, we'll see what happens when fixing one bug leaves you with a project that's still broken.

Fixing a bug

We'll stick with the Dice Battle example one more time. I've introduced more than one bug in the project; here's what happens when we run it now:

$ python dice_battle.py 
  File "dice_battle.py", line 18
    for _ in range(num_battles):
IndentationError: unexpected indent

There's an IndentationError in the main file. Let's take a look at that section of code:

...
# Simulate some battles between players A and B.
num_battles = 10
results = {...}

    for _ in range(num_battles):
    a_result, b_result = utils.battle()
    print(f"\nPlayer A: {a_result}")
    print(f"Player B: {b_result}")
...
dice_battle.py

Here, the for statement is indented along with the body of the loop. Let's unindent the first line of the loop:

...
# Simulate some battles between players A and B.
num_battles = 10
results = {...}

for _ in range(num_battles):
    a_result, b_result = utils.battle()
...
dice_battle.py

That should fix the issue. Let's try running the program again:

$ python dice_battle.py 
Traceback (most recent call last):
  File "dice_battle.py", line 7, in <module>
    import utils
  File "utils.py", line 3, in <module>
    from die import Die
  File "die.py", line 1, in <module>
    import randaom
ModuleNotFoundError: No module named 'randaom'

Oh no, now there's an even longer traceback! Did we mess something up?

This post is only available to paid subscribers at the moment. It will be available to everyone 6 weeks after posting. If you'd like to continue reading now, please support my work by signing up for a paid subscription.

Paid subscription options