Description
Fix the Python script to print the flag. The script has a comparison error.
Setup
Download fixme2.py from the challenge page.
Open it in a text editor.
Solution
- Step 1Identify the bugRun the script. Python 3 raises a SyntaxError because the condition uses = (assignment) instead of == (equality check). Find the line that reads something like: if flag = "":.python3 fixme2.py
Learn more
The assignment operator
=stores a value into a variable. The equality operator==tests whether two values are equal and returns a boolean. Confusing the two is one of the most common beginner programming mistakes across all languages.In Python 3, using
=inside anifcondition is aSyntaxError-- the interpreter catches this at parse time before the script ever runs. This is a deliberate design choice: Python's grammar simply does not allow assignment in that position, preventing a whole class of silent bugs.By contrast, C and C++ do allow assignment inside conditionals --
if (x = 5)silently assigns 5 to x and evaluates as true, which is almost never what the programmer intended. This is why many C style guides recommend writing constants first:if (5 == x)so the compiler catches the mistake. - Step 2Fix the comparison operatorChange the single = to == on the flagged line. The corrected condition reads: if flag == "": -- this tests whether flag is empty rather than attempting to assign to it inside a conditional.
Learn more
Python offers several comparison operators beyond
==:!=-- not equalis-- identity check (same object in memory, not just equal value)is not-- identity inequalityin-- membership test
A subtle but important distinction:
==tests value equality whileistests object identity. For example, two separate empty lists[] == []isTrue, but[] is []isFalsebecause they are different objects. Useisonly when checking againstNone,True, orFalse. - Step 3Run the fixed scriptThe script now runs without error and prints the flag.python3 fixme2.py
Learn more
After correcting the operator, the
ifcondition properly tests whether the flag variable is an empty string. If it is not empty (meaning the flag has been loaded correctly), the script proceeds to print it.This pattern -- checking whether a variable is empty before using it -- is a common defensive programming technique. In real applications, it guards against uninitialized values or missing configuration causing crashes downstream. The bug here simulates a realistic mistake that even experienced developers occasionally make when typing quickly.
Flag
picoCTF{...}
= assigns a value; == tests equality. This is one of the most common beginner Python errors -- Python 3 raises a SyntaxError for = in conditions, unlike some languages that silently allow it.