Learning Python Through the Bugs
A beginner’s honest account of getting stuck, fixing things, and finally understanding what the code was doing.
I sat down with one goal: learn Python by actually building things.
No tutorials. No passive watching. Just a blank Jupyter notebook and a willingness to figure things out.
The interesting part wasn’t the programs I built. It was everything that went wrong while building them — and what those mistakes ended up teaching me.
The Calculator: print() Is Not return
The first challenge was a simple calculator. The logic seemed straightforward: take two numbers, choose an operation, calculate the result, display it.
Then came functions — and with them, one of the most confusing distinctions I encountered early on: the difference between print() and return.
At first they felt almost interchangeable. If the answer appears on the screen, isn’t that what matters? Not quite. print() displays something and forgets it. return sends the value back so the rest of the program can actually use it.
That distinction became much clearer after getting it wrong. My early attempts calculated the result and immediately threw it away:
x1 + x2returnTwo lines. Neither useful. The fix was one line that did both:
return x1 + x2The calculator also introduced try/except — specifically for division by zero. Instead of letting Python crash, the error was caught and handled cleanly. That felt like real control over the program for the first time.
The bigger lesson: a function should do one job. Receive inputs, do the work, return the result. Everything else happens outside.
Fibonacci: When the Logic Is the Hard Part
One lesson that kept coming back throughout this journey is that understanding the logic behind a problem makes finding a solution much easier. Once the logic is clear, the steps follow. Without it, you’re just guessing at syntax.
This became particularly clear while working on a Fibonacci sequence. I went in thinking it would be straightforward — until I realised that translating the pattern into Python required more thought than expected. Specifically, managing how variables update each loop step was trickier than it appeared.
The sequence itself is simple: each number is the sum of the two before it.
0, 1, 1, 2, 3, 5, 8, 13…
But inside the loop, the order of updates matters completely. Update the wrong variable first and the whole sequence breaks. The fix was a temporary variable to hold the value before it got overwritten — the same logic behind swapping two variables.
Get Ethansusuluwa’s stories in your inbox
Join Medium for free to get updates from this writer.
By breaking the problem into smaller steps — calculate, save temporarily, update, repeat — the logic became manageable. That process of decomposition turned out to be more valuable than the solution itself.
The Shopping Cart: Dictionaries and the Loop That Wouldn’t Behave
The shopping cart introduced dictionaries. The natural instinct was to keep item names and prices in separate lists — but that creates a fragile relationship. If one list changes without the other, everything breaks.
A dictionary keeps the relationship explicit:
cart = {'bread': 3.6, 'mangos': 6.4}Item as key. Price as value. Always together.
The bugs here came in layers. An input statement accidentally created a tuple instead of a single value. Prices were treated as strings because they hadn’t been converted to float. The loop ran but nothing accumulated correctly because += was being applied to the wrong type.
Then came .items(), the method that gives both key and value while looping through a dictionary:
for item, price in cart.items():
total += priceOnce that clicked, the rest fell into place. But the real lesson was simpler: if something needs to repeat, it belongs inside the loop. If it only needs to happen once, it belongs outside. Getting that wrong was the source of most of the confusion.
File Handling: The completecomplete Problem
The file handling program introduced three modes — r for read, w for write, a for append — and a bug that took several attempts to understand.
The program collected student names in a loop and wrote them to a file. But instead of names, the file kept showing the word complete. Running it multiple times with append mode made it worse:
completecompleteTwo problems at once. The file-writing operation was inside the loop instead of after it. And the variable being written held its last value, which was ‘complete’, the word typed to stop the loop.
The fix required rethinking the structure entirely. Collect names into a list during the loop. Write the list to the file after the loop ends:
students = []
while True:
name = input('Enter name or complete to stop: ')
if name == 'complete':
break
students.append(name + '\n')
with open('STUDENTS.txt', 'w') as f:
f.writelines(students)Collect first. Write after. That separation is a principle that showed up in every program, not just this one.
What It Actually Taught Me
Looking back, the most useful part wasn’t memorising syntax. It was learning how to think through a problem when the code didn’t behave as expected.
A syntax error tells you Python doesn’t understand what you wrote. A logic error is more interesting: Python understands the code perfectly, but the code doesn’t do what was intended. Both force you to examine your assumptions, and that examination is where the real learning happens.
Programming is not just about writing code. It is about understanding the problem, developing the logic, and then translating that logic into code. As more complex challenges come up, that approach — logic first — will be the foundation everything else builds on.
The blank notebook is no longer blank. And the problems that felt impossible at the start now feel like a natural place to begin.









