In-class exercise Python 4

Exercise · Explain This Python Function (Login Attempt Counter)

Python · Beginner+
Explain this code out loud like you're speaking during a technical interview. Focus on the dictionary, loop, conditional logic, and updating values.
def count_failed_logins(attempts):
    failed_counts = {}

    for username, success in attempts:
        if success == False:
            if username not in failed_counts:
                failed_counts[username] = 1
            else:
                failed_counts[username] += 1

    return failed_counts

login_attempts = [
    ("alice", False),
    ("bob", True),
    ("alice", False),
    ("carla", False),
    ("alice", True)
]

print(count_failed_logins(login_attempts))
🎯 Instructions oral task
  1. Explain what the function does.
  2. Describe what the attempts parameter represents.
  3. Explain why an empty dictionary is created.
  4. Explain how the for loop unpacks each login attempt.
  5. Explain how the function checks for failed logins.
  6. Explain how the dictionary gets updated.
  7. Explain the final output.
📖 Vocabulary core
  • dictionary — a data structure that stores key-value pairs.
  • key-value pair — a relationship where one value is used to look up another value.
  • tuple — an ordered group of values that cannot usually be changed.
  • unpacking — assigning multiple values from a collection into separate variables.
  • conditional logic — code that makes decisions based on conditions.
  • increment — to increase a number by a certain amount.
🧩 Collocations natural English
  • create an empty dictionary
  • loop through login attempts
  • unpack tuple values
  • check whether a login failed
  • add a new key
  • increment the counter
  • return the dictionary
🗣️ Phrasal Verbs interview speech
  • loop through — “The function loops through each login attempt.”
  • keep track of — “The dictionary keeps track of failed login counts.”
  • set up — “We set up an empty dictionary at the beginning.”
  • build up — “The function builds up the dictionary as it processes the data.”
  • come back with — “The function comes back with the final counts.”
🎤 Model Answer spoken

This function is called count_failed_logins. It takes one parameter called attempts, which represents a list of login attempts.

Each login attempt is stored as a tuple with two values: a username and a boolean value showing whether the login was successful.

Inside the function, an empty dictionary called failed_counts is created. This dictionary is used to keep track of how many failed login attempts each user has.

The for loop goes through each tuple in the list. Python unpacks each tuple into two variables: username and success.

If success is False, the function checks whether the username is already in the dictionary. If the username is not there yet, it adds the username as a new key and sets the value to 1.

If the username already exists in the dictionary, the function increments the value by 1.

In this example, Alice has two failed login attempts, Carla has one failed login attempt, and Bob is not included because his login was successful. The final output is {'alice': 2, 'carla': 1}.