Question Detail

COS 102 If staments Objective Question

Question

Which Python code snippet correctly performs a *comparison* to check if the `age` variable is 18?
Options
A if age = 18:
B if age == 18:
Correct Answer
C if age := 18:
D if age is 18:
Correct Answer

Option B is the correct answer.

Detailed Explanation

Page 14, 'Mistake #1: = vs ==' slide, clearly distinguishes between '=' for Assignment and '==' for Comparison. The 'CORRECT' example uses if age == 18:.

Hint

Remember the fundamental difference between assigning a value and checking for equality. One uses a single character, the other uses two.

Question Info