100 days of code: Day 30 - child class

Last modified on 2020-07-08

Source: 100-days-of-code.

Now that my Superuser child class in users.py is configured to inherit from User, I add a new defcon_level attribute and describe_games method unique to this user …

class User:
    """A simple attempt to represent a user."""


    def __init__(self, first_name, last_name, city, country):
        self.first_name = first_name
        self.last_name = last_name
        self.city = city
        self.country = country
        self.login_attempts = 0
        self.max_login_attempts = 3

[...]

class Superuser(User):
    """Represent special privilegs for a system's superuser."""


    def __init__(self, first_name, last_name, city, country):
        """Initialize attributes of the parent class."""
        super().__init__(first_name, last_name, city, country)
        self.defcon_level = 5


    def describe_games(self):
        """Print a statement describing the list of games."""
        games = ['falken\'s maze', 'black jack', 'gin rummy', 'hearts', 
            'bridge', 'checkers', 'chess', 'poker',] 
        
        print(f"Hello {self.first_name}! "
            + f"Currently at DEFCON LEVEL {self.defcon_level}.")
        print("\nList Games\n")
        for game in games:
            print(f"{game.upper()}")
        
[...]

superuser = Superuser('Foo', 'Bar', 'Bletchley', 'UK')
superuser.describe_user()
superuser.describe_games()

Run …

$ python3 users.py
[...]
Username: Foo Bar
Location: Bletchley, UK
Hello Foo! Currently at DEFCON LEVEL 5.

List Games

FALKEN'S MAZE
BLACK JACK
GIN RUMMY
HEARTS
BRIDGE
CHECKERS
CHESS
POKER

Happy hacking!