Fix the Most Common Beginner Bugs
Most bugs beginners hit belong to a small number of families. Once you know them, you recognize them in seconds, often before you even ask an AI. This lesson covers the four you will meet most, each with a broken example to fix in your browser.
For each one, try to fix it yourself first. If you get stuck, use the hints, or ask an AI using the template from the previous lesson, then check that its explanation matches what you see.
What You'll Learn
- Off-by-one errors in loops
- Mixing up text and numbers
- Functions that forget to return a value
- Logic errors in conditions
- A quick checklist for spotting each family
The four families
Most beginner bugs fall into one of these four families.
| Criteria | What it looks like | Typical cause |
|---|---|---|
| Off-by-one | A loop runs once too often or too few times, NaN or IndexError | Using <= instead of <, or starting at 1 instead of 0 |
| Text vs number | TypeError, or "5" + "3" gives "53" | Input read as text and never converted |
| Missing return | A function gives None or undefined | The function calculates but never returns |
| Wrong condition | Runs without error but the result is wrong | > instead of >=, or and instead of or |
What it looks like
- Off-by-one
- A loop runs once too often or too few times, NaN or IndexError
- Text vs number
- TypeError, or "5" + "3" gives "53"
- Missing return
- A function gives None or undefined
- Wrong condition
- Runs without error but the result is wrong
Typical cause
- Off-by-one
- Using <= instead of <, or starting at 1 instead of 0
- Text vs number
- Input read as text and never converted
- Missing return
- The function calculates but never returns
- Wrong condition
- > instead of >=, or and instead of or
1. Off-by-one
Lists start counting at 0. A list with 3 items has positions 0, 1, and 2. Loop one step too far and you reach a position that does not exist. Python raises an IndexError. JavaScript quietly gives undefined, which turns any calculation into NaN ("not a number").
This JavaScript function should return the average of the numbers. Instead it prints NaN. Fix it so it prints 4.
2. Text vs number
Values that come from user input, files, or web forms usually arrive as text, even when they look like numbers. "17" is text. 17 is a number. Doing math with text either fails or gives strange results.
This Python code should print how many years until someone turns 18. It crashes with a TypeError. Fix it so it prints Years until 18: 1.
3. Missing return
A function can do the right calculation and still give back nothing, because it never returns the result. In Python, a function without a return gives None. In JavaScript, it gives undefined. The error then appears later, where the result is used, which can be confusing.
This function should add 10 percent tax to a price. It crashes. Fix it so it prints Final price: 22.0.
4. Wrong condition
These are the sneakiest, because nothing crashes. The program just makes the wrong decision in some cases, often at a boundary: exactly 18, exactly 90, exactly zero. The final lesson includes one of these, since they are also the bugs AI fixes most often get subtly wrong.
Quick ways to catch them:
- Test the exact boundary values, not just typical ones.
- Read each condition out loud: "greater than 90" is not the same as "90 or more."
- Check
andvsor: "age over 12 and under 20" is not "age over 12 or under 20."
A quick spotting checklist
When something goes wrong, run through this before anything else:
- Error mentions an index or position, or you see NaN? Suspect off-by-one.
- TypeError, or numbers joined like "53"? Suspect text vs number.
- None or undefined where you expected a value? Suspect a missing return.
- No error, wrong answer for some inputs? Suspect a condition, and test the boundaries.
Key Takeaways
- Off-by-one: lists start at 0, so loop with
<the length, not<=. - Text vs number: input often arrives as text. Convert with
int(),float(), orNumber()before doing math. - Missing return: a function that calculates but does not
returngivesNoneorundefined. - Wrong condition: test boundary values and read conditions out loud.
- Recognizing the family of a bug tells you where to look, with or without AI.

