Write a Python function that accepts a username as input and raises an exception if the username contains any special characters.
Example 1:
Input: "user@name" Output: "Error: Username should not contain any special characters."
Example 2:
Input: "username" Output: "Valid username."
Use Python’s built-in functions to check if each character in the string is alphanumeric.
def validate_username(username): if not username.isalnum(): raise Exception("Error: Username should not contain any special characters.") else: return "Valid username." print(validate_username("user@name")) # Output: "Error: Username should not contain any special characters." print(validate_username("username")) # Output: "Valid username."
Unlock AI & Data Science treasures. Log in!