Skip to content

Register App Users

Watcharasak Sawatdisan
Data Analyst
Dec 8, 2024

You are a junior developer working in a small start-up. Your managers have asked you to develop a new account registration system for a mobile app. The system must validate user input on the sign-up form before creating an account.

The previous junior developer wrote some helper functions that validate the name, email, and password. Use these functions to register users, store their data, and implement some error handling! These have been imported into the workspace for you. They will be a great help to you when registering the user, but first you have to understand what the function does! Inspect the docstrings of each of the helper functions: validate_name, validate_email and validate_password.

# Re-run this cell and examine the docstring of each function
from python_functions import validate_name, validate_email, validate_password, top_level_domains

print("validate_name\n")
print(validate_name.__doc__)
print("--------------------\n")

print("validate_email\n")
print(validate_email.__doc__) 
print("--------------------\n")

print("validate_password\n")
print(validate_password.__doc__)

# The top level domains variable is used in validate_email to approve only certain email domains
print(top_level_domains)

Validation function

from python_functions import validate_name, validate_email, validate_password, top_level_domains

def validate_user(name, email, password):
    """Validate the user name, email and password.

    Args:
        name (string): Name that we're attempting to validate.
        email (string): Email address that we're attempting to validate.
        password (string): Password that we're attempting to validate.

    Returns:
        Boolean: Will return True if all validation checks pass

    Raises ValueError if validation fails.
    """
    
    if validate_name(name) == False:
        raise ValueError("Please make sure your name is greater than 2 characters!")
    if validate_email(email) == False:
        raise ValueError("Your email adress is in the incorrect format, please enter a valid email.")
    if validate_password(password) == False:
        raise ValueError("Your password is too weak, ensure thet you password is greater than 8 characters, contains a capital letter and a number.")
        
    return True
help(validate_user)

Registration function

from python_functions import validate_name, validate_email, validate_password, top_level_domains

def register_user(name, email, password):
    """Attempt to register the user if they pass validation.

    Args:
        name (string): Name of the user
        email (string): Email address of the user
        password (string): Password of the user

    Returns:
        Dict: Return a dictionary with the user details

    Raises ValueError on missing arguments.
    """
    
    if validate_user(name, email, password):
        user = {
            "name": name,
            "email": email,
            "password": password
        }
        return user

    return False
help(register_user)

Conclusion

In this project, we created a user registration system in Python. We defined a function register_user that takes a user's name, email, and password, validates them, and registers the user if all validations pass. This project helped us understand how to structure a Python function, handle arguments, and return values. We also learned how to use helper functions for validation and how to document our code using docstrings.

Key Learnings

  1. Function Definition: We learned how to define a function in Python using the def keyword and how to pass arguments to it.
  2. Validation: We understood the importance of validating user input and how to use helper functions to keep our code clean and modular.
  3. Error Handling: We learned how to raise exceptions like ValueError to handle missing or invalid arguments.
  4. Documentation: We practiced writing docstrings to document our functions, making our code easier to understand and maintain.
  5. Returning Values: We saw how to return values from a function, including complex data types like dictionaries.

Overall, this project was a great introduction to some fundamental concepts in Python programming. Happy coding!