Problem overview
You write a class Phone that guards a PIN. It stores the PIN and counts wrong tries, and the outside world may only call three things: the constructor Phone(pin), is_locked() and unlock(attempt).
The rules, in the order they matter:
- After 3 wrong tries the phone is locked for good.
- On a locked phone,
unlockreturnsFalsefor every attempt, even the right PIN. - On an unlocked phone, the right PIN returns
Trueand changes nothing. - Any other attempt returns
Falseand counts as one more wrong try.
Take the PIN "0042" and the attempts "4200", "0000", "0041". All three are wrong, so the third one locks the phone. The next call, unlock("0042"), uses the right PIN but still returns False. The tests print False False False True False for the calls unlock, unlock, unlock, is_locked, unlock.
Approach
The class needs two pieces of state: the PIN and a counter of wrong tries. Python has no real private fields, so the convention is a leading underscore: self._pin and self._wrong_tries tell other programmers "don't touch this, use the methods". The language doesn't enforce it, but code that follows the convention keeps the rules in one place.
The only design choice is where to write the "3 wrong tries" rule. Put it in is_locked and let unlock call that method. If the number 3 ever changes, there is one line to edit.
Step by step
- Store the PIN in
self._pininside__init__. - Start the counter: a new phone has
self._wrong_tries = 0. - Write
is_lockedas a single comparison: the counter is at least 3. A comparison already isTrueorFalse, so noifis needed. - Refuse a locked phone first. At the top of
unlock, askself.is_locked()and returnFalse. This check must come before the PIN comparison, or a locked phone would accept the right PIN. - Accept the right PIN. If
attempt == self._pin, returnTrueand leave the counter alone. - Count anything else. Add 1 to
self._wrong_triesand returnFalse.
The solution in Python
class Phone:
def __init__(self, pin):
self._pin = pin
self._wrong_tries = 0
def is_locked(self):
return self._wrong_tries >= 3
def unlock(self, attempt):
if self.is_locked():
return False
if attempt == self._pin:
return True
self._wrong_tries += 1
return False
Using it like the tests do:
phone = Phone("7777")
print(phone.unlock("1234"), phone.unlock("7777"), phone.unlock("0000"),
phone.unlock("7777"), phone.unlock("9999"), phone.is_locked())
False True False True False True
The right PIN worked twice because it never touches the counter. The wrong tries "1234", "0000" and "9999" are the three that lock the phone.
Python details
- The PIN is a string, not a number. Strings keep leading zeros:
"0042"is a four-digit PIN, whileint("0042")is42. Comparing"0042"with42givesFalse, because a string never equals an integer. self._wrong_tries += 1is shorthand forself._wrong_tries = self._wrong_tries + 1.- The underscore is only a convention. Code outside can still write
phone._wrong_tries; the convention says it shouldn't.
Edge cases and common mistakes
- Checking the PIN before the lock. Then a locked phone opens with the right PIN. Example 2 catches this.
- Resetting the counter on success. With
self._wrong_tries = 0in the success branch, the pattern wrong, right, wrong, right, wrong never locks the phone: for PIN"7777"the attempts"1","7777","2","7777","3"leaveis_locked()atFalse, while the lesson's rules lock it. Example 3 catches this. - Counting tries on a locked phone. Harmless to the answers, but the early
return Falsemakes it unnecessary. - Locking at 2 or at 4. Off-by-one in
>= 3. The phone must be unlocked after 2 wrong tries and locked after the third. - Leaving the fields public. Naming them
pinandwrong_triesworks in the tests, but it invites other code to read the PIN or reset the count.
