Skip to main content

Unit 6 · Class is in Session · Lesson 51 of 57

Project: Lockout

Project time!

Remember cracking Alex's PIN by trying all 1000 of them? The fix was to lock people out after a few wrong tries.

Let's build that, properly this time, with a class.

What it should do

Make a class called Account:

__init__(self, username, password)

  • stores the username and password
  • starts self.attempts at 0
  • starts self.locked as False

login(self, password) returns one of three strings:

If...Then...Return
the account is lockeddon't even check the password"Locked"
the password is rightreset attempts back to 0"Welcome"
the password is wrongadd 1 to attempts, and lock the account if that's 3 wrong in a row"Wrong"

So for an account with the password purple4life:

amer = Account("amer", "purple4life")

amer.login("amer123")       # "Wrong"
amer.login("amer1234")      # "Wrong"
amer.login("amer12345")     # "Wrong" (and now it's locked)
amer.login("purple4life")   # "Locked" (too late, Amer)

Once it's locked, even the right password gets "Locked". That's the whole point!

Build the Account class!

Python exercise

Write the program in an editor next to the lesson, run it, and have it checked automatically. Create a free account to do this part.

Do this lesson for real

With a free account you get the terminals, challenges and checks that go with the lessons in Intro to Python, and your progress is saved as you go. CyberAcad is a 501(c)(3) nonprofit, so there is nothing to pay.