# COMMON NESTED LOOP MISTAKES
# Each block shows the wrong way, the error it produces, and the right way
# Read through — don't just run it top to bottom

# ─────────────────────────────────────────────
# MISTAKE 1 — Wrong indentation
# ─────────────────────────────────────────────

# Wrong — print is outside the inner loop
for i in range(1, 4):
    for j in range(1, 4):
        result = i * j
    print(result)    # prints only the last j value

# Right — print inside the inner loop
for i in range(1, 4):
    for j in range(1, 4):
        result = i * j
        print(result)

# ─────────────────────────────────────────────
# MISTAKE 2 — break stops the wrong loop
# ─────────────────────────────────────────────

words = "red horn bull".split()
target = "o"

# Wrong — break stops inner only, outer keeps going
for word in words:
    for letter in word:
        if letter == target:
            print(f"Found in '{word}'")
            break

# Right — flag variable to stop both loops
found = False
for word in words:
    for letter in word:
        if letter == target:
            print(f"Found in '{word}'")
            found = True
            break
    if found:
        break

# ─────────────────────────────────────────────
# MISTAKE 3 — Resetting the counter in the wrong place
# ─────────────────────────────────────────────

word = "Bull"
vowels = "aeiou"

# Wrong — count resets on every inner iteration
for letter in word:
    for vowel in vowels:
        count = 0    # resets every time
        if letter == vowel:
            count += 1

# Right — define before the outer loop
count = 0
for letter in word:
    for vowel in vowels:
        if letter == vowel:
            count += 1

# ─────────────────────────────────────────────
# MISTAKE 4 — Losing track of iterations
# ─────────────────────────────────────────────

# Wrong — 1000 iterations
# for i in range(10):
#     for j in range(10):
#         for k in range(10):
#             print(i, j, k)

# nested loops multiply — keep sequences small, levels shallow
# two levels is usually the limit

# ─────────────────────────────────────────────
# MISTAKE 5 — Same variable name in both loops
# ─────────────────────────────────────────────

# Wrong — inner i overwrites outer i
# for i in range(3):
#     for i in range(3):
#         print(i)

# Right — different names at each level
for i in range(3):
    for j in range(3):
        print(i, j)

# ─────────────────────────────────────────────
# Quick reference
# ─────────────────────────────────────────────

# indentation defines which loop a line belongs to
# break stops only its own loop — use a flag for the outer
# define accumulators before the loop that uses them
# nested loops multiply iterations — keep them small
# use different variable names at each level
