Editor

Spot PII Before You Post

Intermediate · Write the code · from Safe Computing: Privacy, Passwords, Encryption and Phishing

From the lesson: How do attackers get in?

- Malware is software meant to damage a computer system or take partial control of it. Ransomware locks your files until you pay; spyware secretly watches what you do. - A computer virus is a malicious program that can copy itself and get into a computer without permission.

Read this part of the lesson

Write has_pii(text) that returns True if a message contains an email address or a phone number, and False otherwise. Check each word (split on spaces) after stripping the characters .,!? from its ends:

  • An email address has exactly one @, at least one character before it, and a . somewhere after it.
  • A phone number is three groups of digits joined by dashes, with 3, 3 and 4 digits, like 555-867-5309.
  • has_pii("Call me at 555-867-5309 tonight")  ->  True
    has_pii("Meet at 5pm in room 204")          ->  False
    def has_pii(text):
    for word in text.split():
    word = word.strip(".,!?")
    # Return True if word is an email address or a phone number
    return False

    What Check runs

    Your code runs first, then each of these lines, and 2 more that stay hidden. The output must match exactly.

    RunsExpected output
    print(has_pii("Call me at 555-867-5309 tonight"))True
    print(has_pii("My email is sam.lee@example.com."))True
    print(has_pii("Meet at 5pm in room 204"))False

    More Safe Computing exercises

    All Python exercises · 50 practice problems