Description
Fix the Python script to print the flag. The script has a comparison error.
Setup
Download fixme2.py from the challenge page.
Solution
Want to try it yourself first?
The guided walkthrough reveals hints one step at a time.
Step 1
Identify the bugObservationI noticed the challenge description mentioned a comparison error in the script, which suggested running it first to let Python's own error reporting pinpoint the exact line and operator causing the problem.Run 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 = "":.pythonpython3 fixme2.pyExpected output
File "fixme2.py", line 7 if flag = "": ^ SyntaxError: invalid syntaxWhat didn't work first
Tried: Run the script with python2 instead of python3 to see if it works without the error.
Python 2 does allow assignment in some contexts but still raises SyntaxError for = inside an if condition - this is not a Python-version issue. The bug is a logical error (wrong operator) that must be fixed in the source file regardless of which interpreter you use.
Tried: Comment out the if block entirely so the script skips the check and prints the flag unconditionally.
The if block controls whether the flag is printed, so commenting it out may print nothing or cause an indentation error depending on what follows. The correct fix is to change = to == so the condition logic works as intended rather than bypassing it.
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 2
Fix the comparison operatorObservationI noticed the SyntaxError pointed to an if condition using = (assignment) instead of a comparison operator, and looking at what the if block does (prints the flag) told me the condition needed != (not empty) so the block runs only when the flag is present.Open fixme2.py in nano. Change the single = to != on the flagged line. The corrected condition tests whether the flag string is not empty, so the block runs (and prints the flag) only when the flag has been loaded correctly.bashnano fixme2.pyWhat didn't work first
Tried: Use the is operator instead of == to fix the condition, writing: if flag is "":
The is operator checks object identity (same object in memory), not value equality. In CPython some interned empty strings may pass, but this is an implementation detail and not reliable. The correct operator for comparing string values is ==, not is.
Tried: Fix the condition by writing == so the script prints the flag when the variable is empty.
The if block in this script prints the flag, so the condition should pass when the flag is present (not empty). Using == would mean the block only runs when the flag string is empty, which would never print a loaded flag. The correct operator here is !=.
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 3
Run the fixed scriptObservationI noticed the only reported error was the syntax mistake in the if condition, so once that single operator was corrected there were no remaining issues and re-running the script should print the flag.The script now runs without error and prints the flag.pythonpython3 fixme2.pyLearn 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
Reveal flag
picoCTF{3qu4l1ty_n0t_4551gnm3nt_...}
= 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.