← Back

Home β€Ί Problems β€Ί Trying to modify a tuple

Trying to modify a tuple

πŸ› Fix the bug Topic: tuples Difficulty: easy Free

What this problem practises

Tuples are immutable containers that may themselves hold mutable objects, and it is the comma β€” not the parentheses β€” that builds one. Any unpacking where the counts disagree raises ValueError.

This is a fix-the-bug problem: a short program is given in its broken state, and the task is to repair the code so it produces the required output. The interactive editor runs your fixed version in the browser and checks it for you β€” no setup, no local interpreter.

Difficulty: easy β€” the fix or the trick is normally a single line, and a careful read should be enough.

The challenge

This should update a coordinate, but crashes. Fix it.

The code runs in PyDebug’s in-browser compiler. Open the problem, run the snippet, watch the exact error or output, and keep iterating until it is right. Signed-in solves count toward XP and your daily streak.

The error it raises

Run the broken snippet and Python stops with this exact error: TypeError: 'tuple' object does not support item assignment. Match it to the Python errors guide, then repair the line that produced it β€” knowing the message is half the fix.

The snippet

The interactive editor pre-fills exactly this code, broken and ready to repair:

point = (3, 4)
point[0] = 10
print(point)

Understand the rule

TypeError: 'tuple' object does not support item assignment enforces the whole point of tuples: once built, their slots never change. point[0] = 5 fails no matter what the tuple holds. The repair is a design decision β€” either the data was meant to change (use a list) or a modified copy should be built (construct a new tuple).

How to trace it

  1. Find the assignment into an index: name[i] = value on something created with parentheses or commas.
  2. Decide mutability: does this data logically change over time? If yes, create it as a list instead.
  3. If the tuple should stay a tuple, build a replacement: new = old[:i] + (value,) + old[i+1:].
  4. Remember the one-element tuple needs a comma β€” (value,) not (value).

Common wrong fixes

Rule to remember

Tuples are immutable by design: item assignment always raises TypeError. Choose a list when the data changes, or build a new tuple when a modified copy is what you mean.

Solve it in the interactive editor β†’

More tuples problems

Keep going