Challenge - 5 Problems
Python Comments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of code with inline comments
What is the output of this Python code with inline comments?
Python
x = 5 # assign 5 to x # next line multiplies x by 2 y = x * 2 print(y) # print the value of y
Attempts:
2 left
💡 Hint
Look at the calculation done on x before printing y.
✗ Incorrect
The comment lines do not affect the code execution. x is 5, y is x*2 which is 10, so print(y) outputs 10.
❓ Predict Output
intermediate2:00remaining
Effect of comments on code execution
What will be printed by this code?
Python
a = 3 # a = 10 print(a)
Attempts:
2 left
💡 Hint
Check which line is commented out.
✗ Incorrect
The line 'a = 10' is commented out, so it does not run. a remains 3, so print(a) outputs 3.
❓ Predict Output
advanced2:00remaining
Output with multi-line string used as comment
What is the output of this code?
Python
''' This is a multi-line string used as a comment ''' print('Hello')
Attempts:
2 left
💡 Hint
Multi-line strings not assigned to a variable are ignored by Python.
✗ Incorrect
The triple quotes create a multi-line string that is not assigned or used, so it acts like a comment. Only 'Hello' is printed.
❓ Predict Output
advanced2:00remaining
Effect of comments inside code block
What will this code print?
Python
for i in range(3): # print(i) print(i + 1)
Attempts:
2 left
💡 Hint
Look at which print statement is active inside the loop.
✗ Incorrect
The first print is commented out, so only print(i + 1) runs, printing 1, 2, 3 on separate lines.
🧠 Conceptual
expert2:00remaining
Identifying error caused by misplaced comment
Which option will cause a SyntaxError due to incorrect comment placement?
Attempts:
2 left
💡 Hint
Comments cannot start inside a string or expression.
✗ Incorrect
Option A tries to start a comment inside the parentheses before the string ends, causing a SyntaxError.