Question 8
Back to Exercise
Indian PIN codes have exactly 6 digits, but "5600341" is reported as valid. Which statement correctly identifies the bug?
import re
for pin in ["560034", "5600341", "PIN560034"]:
print(pin, "valid" if re.search(r"\d{6}", pin) else "invalid")
- A \d{6} means at least 6 digits
- B search finds 6 digits anywhere inside the string; validate the whole string with re.fullmatch(r"[1-9]\d{5}", pin) or anchors ^…$ Correct
- C search ignores letters
- D PIN codes must be ints
Correct Answer: Option B
Validation needs fullmatch or ^ and $ so that nothing extra is allowed.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!