The function sum_list should return the sum of all elements in a list. Something is wrong — it seems to miss the last element. Find the bug and fix it. Don't use sum() built-in — you must iterate manually.
The function find_max should return the largest number in a list. When you call it, it always returns None. The logic looks right at first glance — but something is fundamentally broken about where the return statement lives. Fix it without changing the algorithm.
The function is_prime(n) should return True if n is a prime number, False otherwise. There are two bugs hidden in this function. One causes incorrect results for small inputs. The other makes the function silently accept some composite numbers as prime. Find and fix both bugs.
The function append_to should take an element and an optional list, append the element to it, and return the result. When called without a second argument, it should always start fresh with an empty list. But something strange is happening — it seems to remember values between calls. How?
The function average(numbers) computes the mean of a list, and passing_grade(scores) uses it to decide if a student passes (average >= 60). A student with scores [60, 70, 80] — average 70 — should clearly pass. But the function says 'fail'. The arithmetic logic looks correct. Where is the counter going wrong?
The function make_multipliers(n) should return a list of n functions, where the i-th function multiplies its argument by i. So make_multipliers(4)[2](5) should return 10 (2 × 5). But every single function in the list behaves identically — they all multiply by the same number. This is one of Python's most infamous gotchas. The code looks right. But it isn't.
The function remove_negatives(numbers) should return a list with all negative numbers removed. It seems to work for some inputs, but with others some negatives survive even though the code clearly checks every element. This is a particularly nasty bug — the code looks completely correct. The logic is right. Yet the output is wrong.
The function count_positives_and_negatives(numbers) takes a list of numbers and returns a tuple (pos, neg) — the count of positive numbers and the count of negative numbers. Zeros are excluded. For most inputs it returns the right count for positives, but the negative count always comes out as 0, no matter what's in the list. The logic looks correct. The filter is right. The counting loops are right. So why does one of them always return zero?
The function find_in_list(items, target) should return the index of the first element in items that is equal in value to target. It returns -1 if not found. It works for most cases — but searching for True in a list containing 1, or False in a list containing 0, mysteriously returns -1 even though those values should match. The comparison line looks perfectly reasonable. What is Python *actually* checking?
The function is_right_triangle(a, b, c) returns True if a triangle with sides a, b, c is a right triangle. The sides can be given in any order. It works perfectly for integer sides like 3, 4, 5. But with decimal values like 0.3, 0.4, 0.5 — a perfectly valid right triangle — it returns False. The formula is correct. The math is correct. So why does it fail?
The function get_median(numbers) should return the median value of a list of numbers. For odd-length lists, the median is the middle element; for even-length lists, it is the average of the two middle elements. The function crashes immediately on any input with a TypeError. Can you find the single-word fix that makes it work?
The function find_max(lst) should recursively find and return the maximum value in a non-empty list. When the largest element happens to be at index 0, it works. For any other list — where the maximum is somewhere in the middle or at the end — it returns None. The recursive logic is correct. The base case is correct. There is exactly one line missing.
The function running_product(numbers) should return a new list where each element is the product of all elements up to and including that index. So [2, 3, 4] should produce [2, 6, 24] — because 2, then 2×3=6, then 6×4=24. Instead, it always returns a list of zeros, regardless of the input. The loop is correct. The logic is correct. There is one wrong starting value.
The function average(a, b, c) should return the arithmetic mean of three numbers: add them all up and divide by three. The formula is written right there on one line. But for most inputs, the result is way off — usually too high. Can you spot where the math breaks down?
The function toggle_case_first(s) should return a new string where the first character's case is toggled — uppercase becomes lowercase, lowercase becomes uppercase. The rest of the string stays the same. The function crashes immediately with a TypeError. Why can't Python do what the code is asking?
The function word_frequency(words) should take a list of words and return a dictionary mapping each word to how many times it appears. It crashes on the very first word every single time. The error happens on a line that looks perfectly logical. Why does Python refuse to increment something that doesn't exist yet?
The function create_board(size) creates a square game board filled with zeros and places a 1 in the center cell. Only the center cell should be 1 — all other cells should be 0. Instead, every row in the board gets the 1, not just the middle one. Changing one cell somehow changes all of them. The board-creation line looks completely normal. What is * size actually doing to the rows?
The function rank_players(scores) takes a list of (name, score) tuples — already sorted highest to lowest — and returns a list of ranking strings like "1. Alice: 95", "2. Bob: 87". The function runs without errors, but all the rank numbers are off by one. Every player shows rank 0, 1, 2... instead of 1, 2, 3. One small parameter change is all it takes.
The function is_positive(n) should return True if n is strictly greater than zero, and False for zero and all negative numbers. It works correctly for positive numbers and for zero. But it incorrectly returns True for negative numbers. In Python, many values are considered "truthy" — but truthy and positive are not the same thing.
The function product_of_list(numbers) should return the product of all numbers in a list. It delegates the actual multiplication to a helper multiply_all(*args), which takes individual numbers as separate arguments. The function always crashes with a TypeError. The helper multiply_all is correct — the bug is in how product_of_list calls it. Can you spot the two missing characters?