Check a username

Here, a space means the literal character " ". The task does not impose other character restrictions.

Write a Python function that takes a username as input and returns "Valid" if the username contains at least 6 characters and does not contain spaces, and "Invalid" otherwise.

Complete Listing 1. Save the completed code as check_username_validity.py and run it to check the supplied assertions.

Listing 1: Starter function and checks for check_username_validity.
def check_username_validity(username: str) -> str:
    raise NotImplementedError("Replace this line with your code.")


# Longer than the minimum, no spaces
got = check_username_validity("john_doe")
assert got == "Valid", f"'john_doe': expected 'Valid', got {got!r}"

# Exactly 6 characters: "at least 6" includes 6
got = check_username_validity("abcdef")
assert got == "Valid", f"'abcdef': expected 'Valid', got {got!r}"

# One character short
got = check_username_validity("abcde")
assert got == "Invalid", f"'abcde': expected 'Invalid', got {got!r}"

# Long enough, but contains a space
got = check_username_validity("user name")
assert got == "Invalid", f"'user name': expected 'Invalid', got {got!r}"

# Your case: which username is most likely to break it?
# got = check_username_validity("___")
# assert got == "___", f"'___': expected '___', got {got!r}"
  • assert condition checks a Boolean condition. A true condition produces no output; a false condition raises AssertionError. For example, assert 2 + 3 == 5 passes.
  • len(text) returns the number of characters in a string. For example, len("abcdef") is 6; len("") is 0.